Promise,相信每个前端工程师都或多或少地在项目中都是用过,毕竟它早已不是一个新名词。ES6中已经原生对它加以支持,在caniuse中搜索一下Promise
,发现新版的chrome和firefox也已经支持。可是低版本的浏览器咱们可使用es6-promise
这个polyfill
库来加以兼容。前端
暂且不谈await
、async
,在Google或百度或360搜索等搜索引擎、或者在segmentfault等社区中,咱们能够搜到一大把介绍promise
的文章,毕竟它已经出现了很长时间,早已有不少大神分析讲解过。es6
我也看了一些文章,可是感受都没有达到想要的效果。因此决定本身开一个小系列文章学习讲解一下promise的原理,以及实现,最后再谈一谈与之联系密切的Deferred对象。ajax
本文是该系列的第一篇文章,主要先让你们对Promise有一个基本的认识。chrome
Promise的出现,本来是为了解决回调地狱的问题。全部人在讲解Promise
时,都会以一个ajax请求为例,此处咱们也用一个简单的ajax的例子来带你们看一下Promise
是如何使用的。segmentfault
ajax请求的传统写法:数组
getData(method, url, successFun, failFun){ var xmlHttp = new XMLHttpRequest(); xmlHttp.open(method, url); xmlHttp.send(); xmlHttp.onload = function () { if (this.status == 200 ) { successFun(this.response); } else { failFun(this.statusText); } }; xmlHttp.onerror = function () { failFun(this.statusText); }; }
改成promise
后的写法:promise
getData(method, url){ var promise = new Promise(function(resolve, reject){ var xmlHttp = new XMLHttpRequest(); xmlHttp.open(method, url); xmlHttp.send(); xmlHttp.onload = function () { if (this.status == 200 ) { resolve(this.response); } else { reject(this.statusText); } }; xmlHttp.onerror = function () { reject(this.statusText); }; }) return promise; } getData('get','www.xxx.com').then(successFun, failFun)
很显然,咱们把异步中使用回调函数的场景改成了.then()
、.catch()
等函数链式调用的方式。基于promise
咱们能够把复杂的异步回调处理方式进行模块化。浏览器
下面,咱们就来介绍一下Promise
究竟是个什么东西?它是如何作到的?前端工程师
Promise
的原理分析其实promise
原理提及来并不难,它内部有三个状态,分别是pending
,fulfilled
和rejected
。异步
pending
是对象建立后的初始状态,当对象fulfill
(成功)时变为fulfilled
,当对象reject
(失败)时变为rejected
。且只能从pengding
变为fulfilled
或rejected
,而不能逆向或从fulfilled
变为rejected
、从rejected
变为fulfilled
。如图所示:
Promise
实例方法介绍Promise
对象拥有两个实例方法then()
和catch()
。
从前面的例子中能够看到,成功和失败的回调函数咱们是经过then()
添加,在promise
状态改变时分别调用。promise
构造函数中一般都是异步的,因此then
方法每每都先于resolve
和reject
方法执行。因此promise
内部须要有一个存储fulfill
时调用函数的数组和一个存储reject
时调用函数的数组。
从上面的例子中咱们还能够看到then
方法能够接收两个参数,且一般都是函数(非函数时如何处理下一篇文章中会详细介绍)。第一个参数会添加到fulfill
时调用的数组中,第二个参数添加到reject
时调用的数组中。当promise
状态fulfill
时,会把resolve(value)
中的value
值传给调用的函数中,同理,当promise
状态reject
时,会把reject(reason)
中的reason
值传给调用的函数。例:
var p = new Promise(function(resolve, reject){ resolve(5) }).then(function(value){ console.log(value) //5 }) var p1 = new Promise(function(resolve, reject){ reject(new Error('错误')) }).then(function(value){ console.log(value) }, function(reason){ console.log(reason) //Error: 错误(…) })
then
方法会返回一个新的promise
,下面的例子中p == p1
将返回false
,说明p1
是一个全新的对象。
var p = new Promise(function(resolve, reject){ resolve(5) }) var p1 = p.then(function(value){ console.log(value) }) p == p1 // false
这也是为何then
是能够链式调用的,它是在新的对象上添加成功或失败的回调,这与jQuery
中的链式调用不一样。
那么新对象的状态是基于什么改变的呢?是否是说若是p
的状态fulfill
,后面的then
建立的新对象都会成功;或者说若是p
的状态reject
,后面的then
建立的新对象都会失败?
var p = new Promise(function(resolve, reject){ resolve(5) }) var p1 = p.then(function(value){ console.log(value) // 5 }).then(function(value){ console.log('fulfill ' + value) // fulfill undefined }, function(reason){ console.log('reject ' + reason) })
上面的例子会打印出5和"fulfill undefined"说明它的状态变为成功。那若是咱们在p1
的then
方法中抛出异常呢?
var p = new Promise(function(resolve, reject){ resolve(5) }) var p1 = p.then(function(value){ console.log(value) // 5 throw new Error('test') }).then(function(value){ console.log('fulfill ' + value) }, function(reason){ console.log('reject ' + reason) // reject Error: test })
理所固然,新对象确定会失败。
反过来若是p
失败了,会是什么样的呢?
var p = new Promise(function(resolve, reject){ reject(5) }) var p1 = p.then(undefined, function(value){ console.log(value) // 5 }).then(function(value){ console.log('fulfill ' + value) // fulfill undefined }, function(reason){ console.log('reject ' + reason) })
说明新对象状态不会受到前一个对象状态的影响。
再来看以下代码:
var p = new Promise(function(resolve, reject){ reject(5) }) var p1 = p.then(function(value){ console.log(value) }) var p2 = p1.then(function(value){ console.log('fulfill ' + value) }, function(reason){ console.log('reject ' + reason) // reject 5 })
咱们发现p1
的状态变为rejected
,从而触发了then
方法第二个参数的函数。这彷佛与咱们以前提到的有差别啊,p1
的状态受到了p
的状态的影响。
再来看一个例子:
var p = new Promise(function(resolve, reject){ resolve(5) }) var p1 = p.then(undefined, function(value){ console.log(value) }) var p2 = p1.then(function(value){ console.log('fulfill ' + value) // fulfill 5 }, function(reason){ console.log('reject ' + reason) })
细心的人可能会发现,该例子中then
第一个参数是undefined
,且value
值5被传到了p1
成功时的回调函数中。上面那个例子中then
的第二个参数是undefined
,一样reason
值也传到了p1
失败时的回调函数中。这是因当对应的参数不为函数时,会将前一promise
的状态和值传递下去。
promise
含有一个实例方法catch
,从名字上咱们就看得出来,它和异常有千丝万缕的关系。其实catch(onReject)
方法等价于then(undefined, onReject)
,也就是说以下两种状况是等效的。
new Promise(function(resolve, reject){ reject(new Error('error')) }).then(undefined, function(reason){ console.log(reason) // Error: error(…) }) new Promise(function(resolve, reject){ reject(new Error('error')) }).catch(function(reason){ console.log(reason) // Error: error(…) })
咱们提到参数不为函数时会把值和状态传递下去。因此咱们能够在多个then
以后添加一个catch
方法,这样前面只要reject
或抛出异常,都会被最后的catch
方法处理。
new Promise(function(resolve, reject){ resolve(5) }).then(function(value){ taskA() }).then(function(value){ taskB() }).then(function(value){ taskC() }).catch(function(reason){ console.log(reason) })
Promise
的静态方法Promise
还有四个静态方法,分别是resolve
、reject
、all
、race
,下面咱们一一介绍。
除了经过new Promise()
的方式,咱们还有两种建立Promise
对象的方法:
Promise.resolve()
它至关于建立了一个当即resolve
的对象。以下两段代码做用相同:
Promise.resolve(5) new Promise(function(resolve){ resolve(5) })
它使得promise对象直接resolve
,并把5传到后面then
添加的成功函数中。
Promise.resolve(5).then(function(value){ console.log(value) // 5 })
Promise.reject()
很明显它至关于建立了一个当即reject
的对象。以下两段代码做用相同:
Promise.reject(new Error('error')) new Promise(function(resolve, reject){ reject(new Error('error')) })
它使得promise对象直接reject
,并把error传到后面catch
添加的函数中。
Promise.reject(new Error('error')).catch(function(reason){ console.log(reason) // Error: error(…) })
Promise.all()
它接收一个promise对象组成的数组做为参数,并返回一个新的promise
对象。
当数组中全部的对象都resolve
时,新对象状态变为fulfilled
,全部对象的resolve
的value
依次添加组成一个新的数组,并以新的数组做为新对象resolve
的value
,例:
Promise.all([Promise.resolve(5), Promise.resolve(6), Promise.resolve(7)]).then(function(value){ console.log('fulfill', value) // fulfill [5, 6, 7] }, function(reason){ console.log('reject',reason) })
当数组中有一个对象reject
时,新对象状态变为rejected
,并以当前对象reject
的reason
做为新对象reject
的reason
。
Promise.all([Promise.resolve(5), Promise.reject(new Error('error')), Promise.resolve(7), Promise.reject(new Error('other error')) ]).then(function(value){ console.log('fulfill', value) }, function(reason){ console.log('reject', reason) // reject Error: error(…) })
那当数组中,传入了非promise对象会如何呢?
Promise.all([Promise.resolve(5), 6, true, 'test', undefined, null, {a:1}, function(){}, Promise.resolve(7) ]).then(function(value){ console.log('fulfill', value) // fulfill [5, 6, true, "test", undefined, null, Object, function, 7] }, function(reason){ console.log('reject', reason) })
咱们发现,当传入的值为数字、boolean、字符串、undefined、null、{a:1}、function(){}等非promise对象时,会依次把它们添加到新对象resolve
时传递的数组中。
那数组中的多个对象是同时调用,仍是一个接一个的依次调用呢?咱们再看个例子
function timeout(time) { return new Promise(function (resolve) { setTimeout(function () { resolve(time); }, time); }); } console.time('promise') Promise.all([ timeout(10), timeout(60), timeout(100) ]).then(function (values) { console.log(values); [10, 60, 100] console.timeEnd('promise'); // 107ms });
由此咱们能够看出,传入的多个对象几乎是同时执行的,由于总的时间略大于用时最长的一个对象resolve
的时间。
Promise.race()
它一样接收一个promise对象组成的数组做为参数,并返回一个新的promise
对象。
与Promise.all()
不一样,它是在数组中有一个对象(最先改变状态)resolve
或reject
时,就改变自身的状态,并执行响应的回调。
Promise.race([Promise.resolve(5), Promise.reject(new Error('error')), Promise.resolve(7)]).then(function(value){ console.log('fulfill', value) // fulfill 5 }, function(reason){ console.log('reject',reason) }) Promise.race([Promise.reject(new Error('error')), Promise.resolve(7)]).then(function(value){ console.log('fulfill', value) }, function(reason){ console.log('reject',reason) //reject Error: error(…) })
且当数组中有非异步Promise
对象或有数字、boolean、字符串、undefined、null、{a:1}、function(){}等非Promise
对象时,都会直接以该值resolve
。
Promise.race([new Promise((resolve)=>{ setTimeout(()=>{ resolve(1) },100)}), Promise.resolve(5), "test", Promise.reject(new Error('error')), Promise.resolve(7)]).then(function(value){ console.log('fulfill', value) // fulfill 5 }, function(reason){ console.log('reject',reason) }) // fulfill 5
数组中第一个元素是异步的Promise
,第二个是非异步Promise
,会当即改变状态,因此新对象会当即改变状态并把5
传递给成功时的回调函数。
那么问题又来了,既然数组中第一个元素成功或失败就会改变新对象的状态,那数组中后面的对象是否会执行呢?
function timeout(time) { return new Promise(function (resolve) { setTimeout(function () { console.log(time) resolve(time); }, time); }); } console.time('promise') Promise.race([ timeout(10), timeout(60), timeout(100) ]).then(function (values) { console.log(values); [10, 60, 100] console.timeEnd('promise'); // 107ms }); // 结果依次为 // 10 // 10 // promise: 11.1ms // 60 // 100
说明即便新对象的状态改变,数组中后面的promise对象还会执行完毕,其实Promise.all()
中即便前面reject
了,全部的对象也都会执行完毕。规范中,promise对象执行是不能够中断的。
promise
对象即便立马改变状态,它也是异步执行的。以下所示:
Promise.resolve(5).then(function(value){ console.log('后打出来', value) }); console.log('先打出来') // 结果依次为 // 先打出来 // 后打出来 5
但还有一个有意思的例子,以下:
setTimeout(function(){console.log(4)},0); new Promise(function(resolve){ console.log(1) for( var i=0 ; i<10000 ; i++ ){ i==9999 && resolve() } console.log(2) }).then(function(){ console.log(5) }); console.log(3);
结果是 1 2 3 5 4,命名4是先添加到异步队列中的,为何结果不是1 2 3 4 5呢?这个涉及到Event loop,后面我会单独讲一下。