Promise
1 Promise的状态
1.1 Promise对象有三种状态:
pending 初始状态,既不是resolved也不是rejected;
resolved 成功。此时调用onFulfilled;
rejected 失败。此时调用onRejected;
resolved和rejected都可以表示为settled.最初Promise为pending状态,在执行后转为settled状态,而settled状态分为两种:在成功后转为resolved,执行.then(onFulfilled)方法;在失败后转为reject,执行.then(onRejected)或.catch(onRejected),进行异步操作,再返回Promise对象。
2 创建Promise的基本语法:
2.1 使用new来调用Promise的构造器进行实例化
var promise = new Promise(function(resolve, reject) { if(...) { resolve(data); } else { reject(new Error('error')); } }); 注意:new Promise(func)时,func会立即执行。 e.g.1 new Promise(function(resolve, reject) { console.log('#1'); resolve(1);//执行完这行,内部属性[[PromiseStatus]]和[[PromiseValue]]都变了,分别由pending -> resolved, undefined -> 1 console.log('#2'); }).then(function() { console.log('#3'); }); // #1 #2 #3 先执行主线程执行栈中的代码 e.g.2 new Promise(function(resolve, reject) { console.log('#1'); reject(4);//执行完这行,内部属性[[PromiseStatus]]和[[PromiseValue]]都变了,分别由pending -> rejected, undefined -> 4 console.log('#2'); }); // #1 #2 报错:Uncaught (in promise) Error: reject(…) 因为后面没有then/catch提供的onRejected回调,异常没有被消费掉 P.S. reject的参数可以是任意类型的,但一般都传入 Error对象表示错误原因
2.2 Promise.resolve & Promise.reject
Promise.resolve & Promise.reject可以认为是new Promise()的快捷方式,返回值也是一个promise对象,所以可以进行链式调用。
Promise.resolve(4); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 4} 等同于以下代码 new Promise(function(resolve) { resolve(4); }); Promise.reject(4); // Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: 4} //等同于以下代码 new Promise(function(resolve, reject) { reject(4); })
3 Promise.prototype.then(function onFulfilled() {...}, function onRejected() {...})
当resolve(成功)时,会调用onFulfilled函数;reject(失败)时,会调用onRejected函数。若只想处理异常情况的函数,可promise.then(undefined, onRejected),当然更好的选择是用promise.catch()来处理。二者效果相同。
promise.then(function (value) { console.log(value); }).catch(function (error) { console.log(error); }); //等同于以下形式 promise.then(function (value) { console.log(value); }, function (error) { console.log(error); }); e.g.3 new Promise(function(resolve, reject) { resolve(2); }); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 2} e.g.4 new Promise(function(resolve, reject) { resolve(2); }).then(function() { console.log('ha'); }); // ha Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: undefined} e.g.5 new Promise(function(resolve, reject) { resolve(2); }).then(function() { return 'haha'; }); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 'haha'} e.g.6 new Promise(function(resolve, reject) { reject(2); }); // Promise {[[PromiseStatus]]: "rejected", [[PromiseValue]]: 2} 报错:Uncaught (in promise) 2 e.g.7 new Promise(function(resolve, reject) { reject(2); }).then(function() { }, function() { console.log('ha'); }); // ha Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: undefined} e.g.8 new Promise(function(resolve, reject) { reject(2); }).then(function() { }, function() { return 'haha'; }); // Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: "haha"} 注意:要想调用then之后返回的promise对象的[[PromiseValue]]不是undefined,必须return一个值; e.g.7和e.g.8返回对象的状态是 resolved,而不是 rejected.