深入解析ES6中的promise

朴灿烈づ我的快乐病毒、 2021-08-19 16:34 449阅读 0赞

file

作者 | Jeskson来源 | 达达前端小酒馆

什么是Promise

Promise对象是用于表示一个异步操作的最终状态(完成或失败)以及其返回的值。

什么是同步,异步

同步任务会阻塞程序的执行,如alert,for

异步任务不会阻塞程序的执行,如setTimeou

使用Promise,then,catch,finally

Promise.all 和 Promise.race

Promise.resolve 和 Promise.reject

回调与Promise

回调函数,用于请求数据

  1. function backFunction(fn) {
  2. setTimeout(function() {
  3. fn && fn();
  4. }, 1000);
  5. }
  6. // 调用
  7. backFunction(function() {
  8. console.log(1); // 1
  9. backFunction(function() {
  10. console.log(2); // 2
  11. backFunction(function() {
  12. console.log(3); // 3
  13. });
  14. });
  15. });

Promise

  1. function d() {
  2. return new Promise(resolve = {
  3. setTimeout(function() {
  4. resolve(); // resolve成功的时候要做的事情
  5. },1000);
  6. // 1秒后调用resolve(),它是一个函数
  7. })
  8. }
  9. d()
  10. .then(function() {
  11. console.log(1);
  12. return d(); // Promise实例
  13. })
  14. .then(function() {
  15. console.log(2);
  16. return d(); // Promise实例
  17. }).then(function() {
  18. console.log(3);
  19. });

对比回调

  1. // 动画
  2. function moveTo(el, x, y, fn) {
  3. el.style.transform = `translate(${x}px, ${y}px)`;
  4. setTimeout(function() {
  5. fn && fn();
  6. },1000);
  7. }
  8. let el = document.querySelector('div');
  9. document.querySelector('button').addeventListener('click', e
  10. moveTo(el, 100, 100, function() {
  11. console.log(1);
  12. moveTo(el, 200, 200, function() {
  13. console.log(2);
  14. });
  15. })
  16. });
  17. // promise
  18. function moveTo(el,x,y) {
  19. return new Promise(resolve => {
  20. el.style.transform = `translate(${x}px, ${y}px)`;
  21. setTimeout(function() {
  22. resolve();
  23. },1000);
  24. });
  25. }
  26. document.querySelector('button').addEventListener('click', e=>{
  27. moveTo(el,100,100)
  28. .then(function() {
  29. console.log('da');
  30. return moveTo(el, 200, 200);
  31. })
  32. .then(function() {
  33. console.log('da2');
  34. }).then(function() {
  35. console.log('da2');
  36. });
  37. });

信任问题

  1. // 使用第三方库 回调
  2. function method(fn) {
  3. // 回调
  4. setTimeout(function() {
  5. // 回调
  6. fn && fn();
  7. // 有可以有bug,被多调用一次
  8. fn && fn();
  9. },1000);
  10. }
  11. // promise一旦被调用,成功或者是失败后,就不能再被修改
  12. function method() {
  13. return new Promise(resolve => {
  14. setTimeout(function() {
  15. //成功
  16. resolve();
  17. // 再调用就不会执行
  18. resolve();
  19. },1000);
  20. });
  21. }
  22. // 控制反转
  23. function method(fn) {
  24. setTimeout(function() {
  25. // 执行回调
  26. fn && fn.call({a:1, b:2)};
  27. // 改变指向
  28. },1000);
  29. }
  30. function method(fn) {
  31. return new Promise(resolve => {
  32. setTimeout(() => {
  33. resolve();
  34. },1000);
  35. });
  36. }

错误处理

then(resolve, reject)then方法中的第二个回调,是失败的时候要做的事情

catch使用实例的then方法,可以捕获错误

finally不论成功与否,finally中的内容一定会执行

  1. function fn(val) {
  2. return new Promise((resolve, reject) => {
  3. if(val) {
  4. resolve(); // 成功的时候
  5. } else {
  6. reject(); // 失败的时候
  7. }
  8. });
  9. }
  10. fn(false)
  11. .then( () => {
  12. console.log("成功");
  13. }, () => {
  14. console.log("失败");
  15. })
  16. function fn(val) {
  17. return new Promise((resolve, reject) => {
  18. if(val) {
  19. resolve(); // 成功的时候
  20. } else {
  21. reject('404'); // 失败的时候
  22. }
  23. });
  24. }
  25. fn(false)
  26. .then( () => {
  27. console.log("成功");
  28. }, e => {
  29. console.log(e);
  30. })
  31. promise resolve只能传递一个参数,如下:
  32. function fn(val) {
  33. return new Promise((resolve, reject) => {
  34. if(val) {
  35. resolve({name: 'da'}); // 成功的时候
  36. } else {
  37. reject('404'); // 失败的时候
  38. }
  39. });
  40. }
  41. fn(true)
  42. .then( dada => {
  43. console.log(data);
  44. }, e => {
  45. console.log(e);
  46. })

catch会捕获错误,如果在回调中没有对错误进行处理

  1. fn(true)
  2. .then(data => {
  3. console.log(data);
  4. return fn(false);
  5. })
  6. .then( () => {
  7. console.log('da'); // 不会执行,没处理错误
  8. })
  9. .then( () => {
  10. })
  11. .catch(e => {
  12. console.log(e);
  13. return fn(false);
  14. }); // 直接输出到这
  15. 不能保证catch被执行

如果没有对失败做出处理,会报错

  1. fn(true)
  2. .then(data => {
  3. console.log(data);
  4. return fn(false);
  5. })
  6. .catch(e=> {
  7. // 捕获错误
  8. console.log(e);
  9. return fn(false);
  10. })
  11. .finally( () => {
  12. console.log(100);
  13. });

Promise的三种状态

pending为进行中的状态,fulfilled为成功的状态,rejected为失败的状态。状态的改变时不可返的,一旦决议就不能修改(决议,状态的改变为决议),状态只能从pending到fulfilled,或者,从pending到rejected。

Promise.all方法可以把多个promise的实例包装成一个新的promise实例

  1. Promise.all( [promise1, promise2] ) : Promise
  2. 数组中,如果promise都为true,则返回为true,决议为成功
  3. 如果数组中有一个为promise,那么返回的是false,决议为失败
  4. 如果是一个空数组,那么返回为true,决议为成功

模式多个请求的数据

  1. function getData1() {
  2. return new Promise((resolve, reject) => {
  3. setTimeout( () => {
  4. console.log('第一条数据加载成功');
  5. resolve('data1');
  6. },1000);
  7. });
  8. }
  9. function getData2() {
  10. return new Promise((resolve, reject) => {
  11. setTimeout( () => {
  12. console.log('第二条数据加载成功');
  13. resolve('data2');
  14. },1000);
  15. });
  16. }
  17. function getData3() {
  18. return new Promise((resolve, reject) => {
  19. setTimeout( () => {
  20. console.log('第三条数据加载成功');
  21. resolve('data3'); // 改为 reject('err')
  22. },1000);
  23. });
  24. }
  25. let p = Promise.all( [getData1(), getData2(), getData3()] );
  26. p.then(arr => {
  27. console.log(arr);
  28. });
  29. // 失败
  30. p.then(arr => {
  31. console.log(arr);
  32. }, e => {
  33. console.log(e);
  34. });
  35. let p = Promise.all([]); // 决议为成功
  36. p.then( () => {
  37. console.log(`da`);
  38. }, e => {
  39. console.log(e);
  40. });
  41. 第一条数据加载成功
  42. 第二条数据加载成功
  43. 第三条数据加载成功

不用Promise.all

  1. let count = 0;
  2. function getData1() {
  3. setTimeout( () => {
  4. console.log('第一条数据加载成功');
  5. count ;
  6. func();
  7. },1000);
  8. }
  9. function getData2() {
  10. setTimeout( () => {
  11. console.log('第二条数据加载成功');
  12. count ;
  13. func();
  14. },1000);
  15. }
  16. function getData3() {
  17. setTimeout( () => {
  18. console.log('第三条数据加载成功');
  19. count ;
  20. func();
  21. },1000);
  22. }
  23. function getData4() {
  24. setTimeout( () => {
  25. console.log('第四条数据加载成功');
  26. count ;
  27. func();
  28. },1000);
  29. }
  30. // 写一个方法:
  31. function func() {
  32. if(count < 4)return;
  33. console.log('全部拿到了');
  34. }

调用

  1. getData2();
  2. getData3();
  3. getData4();

file

  1. let err = false;
  2. function getData1() {
  3. setTimeout( () => {
  4. console.log('第一条数据加载成功');
  5. if(status) err = true;
  6. count ;
  7. func();
  8. },1000);
  9. }
  10. function func() {
  11. if(count < 4)return;
  12. console.log('全部拿到了');
  13. if(err) {
  14. // ...
  15. }
  16. }

Promise.race()

  1. Promise.race([promise1, promise2]) : Promise
  2. let p = Promise.race([getData1(), getData2(),getData3()]);
  3. p.then (data=>{
  4. console.log(data);
  5. })
  6. let flag = false;
  7. function func(data) {
  8. if(flag) return
  9. flag = true;
  10. console.log(data);
  11. }
  12. function getData1() {
  13. setTimeout(()=>{
  14. console.log("第一条数据加载成功");
  15. func({name: 'da'});
  16. },500);
  17. }
  18. function getData2() {
  19. setTimeout( () => {
  20. console.log("第二条数据加载成功");
  21. func({name: 'dada'});
  22. }, 1000);
  23. }
  24. getData1();
  25. getData2();
  26. 第一条数据加载成功
  27. {name: 'da'}
  28. 第二条数据加载成功

Promise.resolve与Promise.reject

Promise.resolve() 与 Promise.reject()

  1. // Promise.resolve
  2. 传递一个普通的值
  3. let p1 = new Promise(resolve => {
  4. resolve('成功!');
  5. });
  6. let p2 = Promise.resolve('成功!');
  7. // 传递一个promise实例
  8. let pro = new Promise(resolve => {
  9. resolve('da');
  10. });
  11. let p = Promise.resolve(pro);
  12. p.then(data => void console.log(data));
  13. let obj = {
  14. then (cb) {
  15. console.log('da');
  16. da('dada');
  17. },
  18. method() {
  19. console.log('coding');
  20. }
  21. }
  22. // 立即执行
  23. Promise.resolve(obj).then(data => {
  24. console.log(data);
  25. });

Promise异步:

  1. console.log(1);
  2. let p = new Promise(resolve => {
  3. console.log(2);
  4. resolve();
  5. console.log(3);
  6. });
  7. console.log(4);
  8. p.then(()=>{
  9. console.log(5);
  10. });
  11. console.log(6);
  12. // 123465

Promise改善了传统回调造成的代码难维护,控制反转等问题,promise是异步的,如果all接收的是空数组,马上会被决议为成功,如果race接受的是空数组,那么会被永远挂起,无限捕获错误问题。

resove和reject方法:

如果接收的是普通的值,那么就会立即决议为成功,并填充这个值,如果接收的是一个promise实例,那么返回这个promise实例,如果接收的是个thenable对象,则会把它包装成promise对象,并立即执行这个对象的then方法,reject会产生一个决议失败的promise并直接传递值。

JavaScript/ES6 Promise

JavaScript的Promise代表一个操作的结果还没有结果,就是如网络请求操作,当我们从某个数据源获取数据的时候,没有办法确定它什么时候能够返回,接受到响应。

Promise提供标准

  1. doSomething()
  2. .then(doSomethingElse)
  3. .catch(handleError)
  4. .then(doMore)
  5. .then(doFinally)
  6. .catch(handleAnotherError)

创建Promise

一个Promise使用Promise构造器创建,接受两个参数resolve,reject

  1. var promise = new Promise( function(resolve, reject) {
  2. // new Promise resolve() reject()
  3. }
  4. XMLHttpRequestpromise版本:
  5. function get(url) {
  6. return new Promise(function(resolve, reject) {
  7. var req = new XMLHttpRequest();
  8. req.open('GET', url);
  9. req.onload = function() {
  10. if(req.status == 200) {
  11. resolve(req.response);
  12. }else{
  13. reject(Error(req.statusText));
  14. }
  15. };
  16. req.onerror = function() {
  17. reject(Error("Network Error"));
  18. };
  19. req.send();
  20. });
  21. }

使用Promise

  1. get(url)
  2. .then(function(response) {
  3. },function(err) {
  4. })

处理错误:

  1. get(url)
  2. .then(function(response){
  3. }, undefined)
  4. .then(undefined, function(err){
  5. })
  6. get(url)
  7. .then(function(response){
  8. })
  9. .catch(function(err){
  10. })

链式

  1. get(url)
  2. .then(function(response){
  3. response = JSON.parse(response);
  4. var secondURL = response.data.url
  5. return get(secondURL);
  6. })
  7. .then(function(response){
  8. response = JSON.parse(response);
  9. var thirdURL = response.data.url
  10. return get(thirdURL);
  11. })
  12. .catch(function(err){
  13. handleError(err);
  14. });

并行执行Promise

Promise.all()方法每个promise数组成为则决议为成功,如果数组中有任意一个promise为失败则决议为失败。

任务一,任务二,任务三,.then() -> success 任务成功

ES6

Promise对象用于表示一个异步操作的最终状态,以及其返回的值。

语法:

  1. new Promise(function (resolve, reject) {
  2. });

几种状态:

pending初始状态,既不是成功,也不是失败状态;fulfilled意味着操作成功完成;rejected意味着操作失败。

pending状态的Promise对象可能会触发filfilled状态,并传递一个值给响应的状态处理方法,也可能触发失败状态rejected并传递失败信息。

Promise.all(iterable)

这个方法返回一个新的promise对象,该promise对象在itearable参数中,当里面所有的的promise对象决议成功的时候才触发成功,否则里面如何一个promise对象决议失败的时候,立即触发promise对象的失败。

Promise.all方法常用于处理多个promise对象的状态集合。

Promise.race(iterable)

当iterable参数里的任意一个子promise被决议成功或者是决议失败后,父promise会用子promise的成功返回值,或是失败返回。

Promise.reject(reason)

返回一个状态为失败的promise对象,将给定的失败信息传递给对应的处理方法。

Promise.resolve(value)

返回一个状态为失败的promise对象,将给定的失败信息传递给对应的处理方法。

  1. const myPromise = new Promise( (resolve, reject) => {
  2. resolve('resolve'); // filfilled
  3. reject('reject'); // rejected
  4. });
  5. function myFunction(url) {
  6. return new Promise( (resolve, reject) => {
  7. xhr.open ("GET", url);
  8. xhr.onload = () => resolve(xhr.responseText);
  9. xhr.onerror = () => reject(xhr.statusText);
  10. xhr.send();
  11. });
  12. };

当异步代码执行成功的时候,会调用resolve(),当异步代码执行失败的时候,会调用reject()。

模拟异步代码:

  1. setTimeout(function(){
  2. resolve('成功');
  3. },250);
  4. });
  5. myPromise.then(function(successMsg){
  6. });

ES6 Promise对象

Promise对象是异步编程的一种解决方案,语法上,Promise是一个对象,从它那可以获取异步操作的信息。

Promise的状态,promise异步操作有三种状态,pending(进行中),fulfilled(已成功),reject(已失败)。除了异步操作的结果,任何其他操作都是无法改变这个状态。

  1. const p1 = new Promise(function(resolve,reject){
  2. resolve('success1');
  3. resolve('success2');
  4. });
  5. const p2 = new Promise(function(resolve,reject){
  6. resolve('success3');
  7. reject('reject');
  8. });
  9. p1.then(function(value){
  10. console.log(value); // success1
  11. });
  12. p2.then(function(value){
  13. console.log(value); // success3
  14. });

缺点,一旦建立Promise就会立即执行,无法中途取消,如果不设置回调函数,Promise内部会抛出错误,不会反应到外部。

then方法,接收两个函数作为参数。

第一个参数是 Promise 执行成功时的回调,第二个参数是 Promise 执行失败时的回调,两个函数只会有一个被调用。

  1. const p = new Promise(function(resolve,reject){
  2. resolve('success');
  3. });
  4. p.then(function(value){
  5. console.log(value);
  6. });
  7. console.log('first');
  8. // first
  9. // success
  10. const p = new Promise(function(resolve,reject){
  11. resolve(1);
  12. }).then(function(value){ // 第一个then // 1
  13. console.log(value);
  14. return value * 2;
  15. }).then(function(value){ // 第二个then // 2
  16. console.log(value);
  17. }).then(function(value){ // 第三个then // undefined
  18. console.log(value);
  19. return Promise.resolve('resolve');
  20. }).then(function(value){ // 第四个then // resolve
  21. console.log(value);
  22. return Promise.reject('reject');
  23. }).then(function(value){ // 第五个then //reject:reject
  24. console.log('resolve:' value);
  25. }, function(err) {
  26. console.log('reject:' err);
  27. });

then方法将返回一个resolved或是rejected状态的Promise对象用于链式调用。

热Promise

在JavaScript中,所有代码都是单线程的,也就是同步执行的,promise就是为了提供一个解决方案的异步编程。

promise的特点:只有异步操作可以决定当前处于的状态,并且任何其他操作无法改变这个状态;一旦状态改变,就不会在变。

状态改变的过程:从pending变为fulfilled和从pending变为rejected,状态改变后,就不会在改变了,这就叫已定型resolved

用法:

Promise对象是由关键字new及其构造函数来创建的。

  1. const promise = new Promise((resolve, reject) => {
  2. // do something here ...
  3. if (success) {
  4. resolve(value); // fulfilled
  5. } else {
  6. reject(error); // rejected
  7. }
  8. });

函数接收两个函数作为参数,分别是resolve和reject,当异步操作执行成功后,会将异步操作的结果作为参数传入resolve函数并执行,此时的状态由Promise状态从pending变为fulfilled;而失败会将异步操作的错误作为参数传入reject函数并执行,此时Promise对象状态从pending变为rejected。

通过then方法,将指定resolved状态和rejected状态的回调函数。

  1. promise.then(function(value) {
  2. // success
  3. }, function(error) {
  4. // failure
  5. });

Promise.all(iterable),iterable必须是一个可以迭代的对象,如Array

返回值为一个新的Promise实例。

  1. var p1 = new Promise((resolve, reject) => {
  2. setTimeout(resolve, 1000, 'one');
  3. });
  4. var p2 = new Promise((resolve, reject) => {
  5. setTimeout(resolve, 2000, 'two');
  6. });
  7. var p3 = new Promise((resolve, reject) => {
  8. setTimeout(resolve, 3000, 'three');
  9. });
  10. var p4 = new Promise((resolve, reject) => {
  11. reject('p4 reject!');
  12. });
  13. var p5 = new Promise((resolve, reject) => {
  14. reject('p5 reject!');
  15. });
  16. Promise.all([p1, p2, p3, p4, p5]).then(values => {
  17. console.log(values);
  18. }, reason => {
  19. console.log(reason)
  20. });
  21. // p4 reject!

Promise.race(iterable),同理,返回值为一个新的Promise实例。

返回的新实例状态,会是最先改变状态的那个实例,如果不是Promise实例,先用Promise.resolve方法,如果传入的迭代为空,则返回的Promise永久等待。

一个Promise实例原封不动的返回该实例;

  1. var original = Promise.resolve('第二行');
  2. var da = Promise.resolve(original);
  3. da.then(function(value) {
  4. console.log('value: ' value);
  5. });
  6. console.log('original === da ? ' (original === da));
  7. // "original === da ? true"
  8. // "value: 第二行"

跟随这个thenable对象的,采用它的最终状态;

  1. let thenable = {
  2. then: function(resolve, reject) {
  3. resolve(41);
  4. }
  5. }
  6. let p = Promise.resolve(thenable);
  7. p.then(function(value) {
  8. console.log(value);
  9. })
  10. // 41

直接将传入参数当最终结果,并返回一个新的Promise;

  1. let p = Promsie.resolve(12);
  2. p.then(function(number) {
  3. console.log(number);
  4. })
  5. // 12

直接返回一个resolved状态的Promise对象

  1. let p = Promsie.resovle();
  2. p.then(function() {
  3. // do something
  4. })

Promise.prototype.then()

  1. p.then(onResolve, onReject);
  2. p.then(function(value) {
  3. }, function(reason) {
  4. });

Promise.prototype. catch()

  1. p.catch(onReject)
  2. p.catch(function(reason) {
  3. });
  4. // bad
  5. promise
  6. .then(function(data) {
  7. // success
  8. }, function(err) {
  9. // error
  10. });
  11. // good
  12. promise
  13. .then(function(data) {
  14. // success
  15. })
  16. .catch(function(err) {
  17. // error
  18. });

Promise.prototype. finally()

  1. p.finally(onFinally);
  2. p.finally(function() {
  3. })

该回调函数的不接受任何参数

promise是一个对象,代表一个异步操作,有三种状态,进行中,成功,失败。只有异步操作的结果的可以决定当前是哪种状态,promise一旦新建执行,就没有办法中途停止。

Promise.all方法用于将多个Promise实例,包装成一个新的Promise实例。只有当作为参数所有的promise函数运行完毕,才会执行.then回调。

file

file

file

  1. //以往回调方式
  2. 函数1(function(){
  3. //代码执行...(ajax1)
  4. 函数2(function(){
  5. //代码执行...(ajax2)
  6. 函数3(function(data3){
  7. //代码执行...(ajax3)
  8. });
  9. ...
  10. });
  11. });
  12. //Promise回调方式:链式调用,可构建多个回调函数。
  13. promise().then().then()...catch()
  14. //创建Promise实例
  15. let promise = new Promise( (resolve, reject) => {
  16. //执行相应代码 根据情况调用resolve或reject
  17. ...
  18. })
  19. //promise的then方法中执行回调
  20. promise.then(function(){
  21. //第一个参数是返回resolve时
  22. },function(){
  23. //第二个是回调返回reject时
  24. }
  25. }

定时器调用

  1. const promise = new Promise(function(resolve, reject){
  2. setTimeout(resolve,1000);
  3. })
  4. promise.then(function(){
  5. console.log('resolve:成功回调函数')
  6. },function(){
  7. console.log('reject:失败回调函数')
  8. })

file

传递参数:

  1. const promise = new Promise((resolve, reject) => {
  2. setTimeout(reject,1000,'我是value值');
  3. })
  4. promise.then((value) => {
  5. console.log('resolve:' value)
  6. }).catch((value) => {
  7. console.log('reject:' value)
  8. })
  9. //第一种,单个传值是无效的
  10. const promise = new Promise((resolve, reject) => {
  11. setTimeout(resolve,1000,'参数1','参数2');
  12. })
  13. promise.then((value1,value2) => {
  14. console.log('value1:' value1) //value1:参数1
  15. console.log('value2:' value2) //value2:undefined
  16. }).catch((value) => {
  17. console.log(value)
  18. })
  19. //第二种:数组传值
  20. const promise = new Promise((resolve, reject) => {
  21. setTimeout(resolve,1000,['参数1','参数2']);
  22. })
  23. promise.then((value1) => {
  24. console.log('value1:' value1) //value1:参数1,参数2
  25. }).catch((value) => {
  26. console.log(value)
  27. })

Promise.prototype.then方法:链式操作

  1. getJSON("/posts.json").then(function(json) {
  2. return json.post;
  3. }).then(function(post) {
  4. // proceed
  5. });

Promise.prototype.catch方法:捕捉错误

  1. getJSON("/posts.json").then(function(posts) {
  2. }).catch(function(error) {
  3. console.log('发生错误!', error);
  4. });

Promise.all方法,Promise.race方法

  1. var p = Promise.all([p1,p2,p3]);
  2. var p = Promise.race([p1,p2,p3]);

❤️ 不要忘记留下你学习的脚印 [点赞 收藏 评论]

作者Info:

【作者】:Jeskson

【原创公众号】:达达前端小酒馆。

【转载说明】:转载请说明出处,谢谢合作!~

关于目前文章内容即涉及前端,PHP知识点,如果有兴趣即可关注,很荣幸,能被您发现,真是慧眼识英!也感谢您的关注,在未来的日子里,希望能够一直默默的支持我,我也会努力写出更多优秀的作品。我们一起成长,从零基础学编程,将 Web前端领域、数据结构与算法、网络原理等通俗易懂的呈现给小伙伴。分享 Web 前端相关的技术文章、工具资源、精选课程、热点资讯。

#

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。

#

请点赞!因为你们的赞同/鼓励是我写作的最大动力!

欢迎关注达达的CSDN!

这是一个有质量,有态度的博客

7d927f18ebd05ea1d505a572393fbc87.jpg

发表评论

表情:
评论列表 (有 0 条评论,449人围观)

还没有评论,来说两句吧...

相关阅读

    相关 深入解析 ES6:Generator

    今天讨论的新特性让我非常兴奋,因为这个特性是 ES6 中最神奇的特性。 这里的“神奇”意味着什么呢?对于初学者来说,该特性与以往的 JS 完全不同,甚至有些晦涩难懂。从某种意

    相关 ES6-Promise

    Promise what? 在我看来Promise就相当于一个承诺容器,将所要执行的事件存贮在promise容器中。 Promise在ES6中被统一规范,由浏览

    相关 ES6promise法则

    一,promise用来解决什么问题的 > Promise > 为异步编程提供统一的解决方案,比传统的回调和事件更加合理有效。多重嵌套的回调函数,代码是横向发展,不是纵

    相关 ES6 Promise 使用

      Promise 是什么?         Promise 是一个对象,它用于异步计算。它可以将异步操作队列化,按照期望的顺序执行,并返回符合预期的结果。它是异步编程