promise 平台原生浏览器不支持promise么

深入理解 Promise 五部曲 -- 4.扩展问题 | Ghost中文网JavaScript Promise启示录
本篇,主要普及promise的用法。
一直以来,JavaScript处理异步都是以callback的方式,在前端开发领域callback机制几乎深入人心。在设计API的时候,不管是浏览器厂商还是SDK开发商亦或是各种类库的作者,基本上都已经遵循着callback的套路。
近几年随着JavaScript开发模式的逐渐成熟,CommonJS规范顺势而生,其中就包括提出了Promise规范,Promise完全改变了js异步编程的写法,让异步编程变得十分的易于理解。
在callback的模型里边,我们假设需要执行一个异步队列,代码看起来可能像这样:
loadImg('a.jpg', function() {
loadImg('b.jpg', function() {
loadImg('c.jpg', function() {
console.log('all done!');
这也就是我们常说的回调金字塔,当异步的任务很多的时候,维护大量的callback将是一场灾难。当今Node.js大热,好像很多团队都要用它来做点东西以沾沾“洋气”,曾经跟一个运维的同学聊天,他们也是打算使用Node.js做一些事情,可是一想到js的层层回调就望而却步。
好,扯淡完毕,下面进入正题。
Promise可能大家都不陌生,因为Promise规范已经出来好一段时间了,同时Promise也已经纳入了ES6,而且高版本的chrome、firefox浏览器都已经原生实现了Promise,只不过和现如今流行的类Promise类库相比少些API。
所谓Promise,字面上可以理解为“承诺”,就是说A调用B,B返回一个“承诺”给A,然后A就可以在写计划的时候这么写:当B返回结果给我的时候,A执行方案S1,反之如果B因为什么原因没有给到A想要的结果,那么A执行应急方案S2,这样一来,所有的潜在风险都在A的可控范围之内了。
上面这句话,翻译成代码类似:
var resB = B();
var runA = function() {
resB.then(execS1, execS2);
只看上面这行代码,好像看不出什么特别之处。但现实情况可能比这个复杂许多,A要完成一件事,可能要依赖不止B一个人的响应,可能需要同时向多个人询问,当收到所有的应答之后再执行下一步的方案。最终翻译成代码可能像这样:
var resB = B();
var resC = C();
var runA = function() {
.then(resC, execS2)
.then(resD, execS3)
.then(resE, execS4)
.then(execS1);
在这里,当每一个被询问者做出不符合预期的应答时都用了不同的处理机制。事实上,Promise规范没有要求这样做,你甚至可以不做任何的处理(即不传入then的第二个参数)或者统一处理。
好了,下面我们来认识下:
一个promise可能有三种状态:等待(pending)、已完成(fulfilled)、已拒绝(rejected)
一个promise的状态只可能从“等待”转到“完成”态或者“拒绝”态,不能逆向转换,同时“完成”态和“拒绝”态不能相互转换
promise必须实现then方法(可以说,then就是promise的核心),而且then必须返回一个promise,同一个promise的then可以调用多次,并且回调的执行顺序跟它们被定义时的顺序一致
then方法接受两个参数,第一个参数是成功时的回调,在promise由“等待”态转换到“完成”态时调用,另一个是失败时的回调,在promise由“等待”态转换到“拒绝”态时调用。同时,then可以接受另一个promise传入,也接受一个“类then”的对象或方法,即thenable对象。
可以看到,Promise规范的内容并不算多,大家可以试着自己实现以下Promise。
以下是笔者自己在参考许多类Promise库之后简单实现的一个Promise,代码请移步。
简单分析下思路:
构造函数Promise接受一个函数resolver,可以理解为传入一个异步任务,resolver接受两个参数,一个是成功时的回调,一个是失败时的回调,这两参数和通过then传入的参数是对等的。
其次是then的实现,由于Promise要求then必须返回一个promise,所以在then调用的时候会新生成一个promise,挂在当前promise的_next上,同一个promise多次调用都只会返回之前生成的_next。
由于then方法接受的两个参数都是可选的,而且类型也没限制,可以是函数,也可以是一个具体的值,还可以是另一个promise。下面是then的具体实现:
Promise.prototype.then = function(resolve, reject) {
var next = this._next || (this._next = Promise());
var status = this.
if('pending' === status) {
isFn(resolve) && this._resolves.push(resolve);
isFn(reject) && this._rejects.push(reject);
if('resolved' === status) {
if(!isFn(resolve)) {
next.resolve(resolve);
x = resolve(this.value);
resolveX(next, x);
} catch(e) {
this.reject(e);
if('rejected' === status) {
if(!isFn(reject)) {
next.reject(reject);
x = reject(this.reason);
resolveX(next, x);
} catch(e) {
this.reject(e);
这里,then做了简化,其他promise类库的实现比这个要复杂得多,同时功能也更多,比如还有第三个参数——notify,表示promise当前的进度,这在设计文件上传等时很有用。对then的各种参数的处理是最复杂的部分,有兴趣的同学可以参看其他类Promise库的实现。
在then的基础上,应该还需要至少两个方法,分别是完成promise的状态从pending到resolved或rejected的转换,同时执行相应的回调队列,即resolve()和reject()方法。
到此,一个简单的promise就设计完成了,下面简单实现下两个promise化的函数:
function sleep(ms) {
return function(v) {
var p = Promise();
setTimeout(function() {
p.resolve(v);
function getImg(url) {
var p = Promise();
var img = new Image();
img.onload = function() {
p.resolve(this);
img.onerror = function(err) {
p.reject(err);
由于Promise构造函数接受一个异步任务作为参数,所以getImg还可以这样调用:
function getImg(url) {
return Promise(function(resolve, reject) {
var img = new Image();
img.onload = function() {
resolve(this);
img.onerror = function(err) {
reject(err);
接下来(见证奇迹的时刻),假设有一个BT的需求要这么实现:异步获取一个,解析json数据拿到里边的图片,然后按顺序队列加载图片,没张图片加载时给个loading效果
function addImg(img) {
$('#list').find('& li:last-child').html('').append(img);
function prepend() {
.html('loading...')
.appendTo($('#list'));
function run() {
$('#done').hide();
getData('map.json')
.then(function(data) {
$('h4').html(data.name);
return data.list.reduce(function(promise, item) {
return promise
.then(prepend)
.then(sleep(1000))
.then(function() {
return getImg(item.url);
.then(addImg);
}, Promise.resolve());
.then(sleep(300))
.then(function() {
$('#done').show();
$('#run').on('click', run);
这里的sleep只是为了看效果加的,可猛击查看!当然,Node.js的例子可查看。
在这里,Promise.resolve(v)静态方法只是简单返回一个以v为肯定结果的promise,v可不传入,也可以是一个函数或者是一个包含then方法的对象或函数(即thenable)。
类似的静态方法还有Promise.cast(promise),生成一个以promise为肯定结果的promise;
Promise.reject(reason),生成一个以reason为否定结果的promise。
我们实际的使用场景可能很复杂,往往需要多个异步的任务穿插执行,并行或者串行同在。这时候,可以对Promise进行各种扩展,比如实现Promise.all(),接受promises队列并等待他们完成再继续,再比如Promise.any(),promises队列中有任何一个处于完成态时即触发下一步操作。
标准的Promise
可参考html5rocks的这篇文章,目前高级浏览器如chrome、firefox都已经内置了Promise对象,提供更多的操作接口,比如Promise.all(),支持传入一个promises数组,当所有promises都完成时执行then,还有就是更加友好强大的异常捕获,应对日常的异步编程,应该足够了。
第三方库的Promise
现今流行的各大js库,几乎都不同程度的实现了Promise,如dojo,jQuery、Zepto、when.js、Q等,只是暴露出来的大都是Deferred对象,以jQuery(Zepto类似)为例,实现上面的getImg():
function getImg(url) {
var def = $.Deferred();
var img = new Image();
img.onload = function() {
def.resolve(this);
img.onerror = function(err) {
def.reject(err);
return def.promise();
当然,jQuery中,很多的操作都返回的是Deferred或promise,如animate、ajax:
// animate
.animate({'opacity': 0}, 1000)
.promise()
.then(function() {
console.log('done');
$.ajax(options).then(success, fail);
$.ajax(options).done(success).fail(fail);
// ajax queue
$.when($.ajax(options1), $.ajax(options2))
.then(function() {
console.log('all done.');
}, function() {
console.error('There something wrong.');
jQuery还实现了done()和fail()方法,其实都是then方法的shortcut。
处理promises队列,jQuery实现的是$.when()方法,用法和Promise.all()类似。
其他类库,这里值得一提的是,本身代码不多,完整实现Promise,同时支持browser和Node.js,而且提供更加丰富的API,是个不错的选择。这里限于篇幅,不再展开。
我们看到,不管Promise实现怎么复杂,但是它的用法却很简单,组织的代码很清晰,从此不用再受callback的折磨了。
最后,Promise是如此的优雅!但Promise也只是解决了回调的深层嵌套的问题,真正简化JavaScript异步编程的还是Generator,在Node.js端,建议考虑Generator。
下一篇,研究下Generator。
github原文:
JavaScript Promise启示录随笔 - 35&
文章 - 0&评论 - 86&trackbacks - 0
有个东西叫回调地狱,promise就是来解决这个的.
看过的一些相关的技术文章
Promise介绍
Promise介绍
以前写一些异步的东西,都使用回调函数来执行的,这样的写法让人看起来很不舒服
比如写一个隔几秒就执行的一个回调的东西,然后在回调的时候,开始下一个异步
setTimeout(function(){
console.log(11111)
setTimeout(function(){
console.log(22222)
setTimeout(function(){
console.log(33333)
setTimeout(function(){
console.log(44444)
setTimeout(function(){
console.log(55555)
这代码开起来维护起来都会很麻烦的,后来的后来,es6支持原生的Promise,jquery支持Deferred,Promise后,异步问题就容易多了
一个原生的Promise实现上面的代码
function setTime(t){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve();
setTime(1000)
.then(function(){
console.log(11111)
return setTime(1000);
.then(function(){
console.log(22222)
return setTime(2000);
.then(function(){
console.log(33333)
return setTime(3000);
.then(function(){
console.log(44444)
return setTime(4000);
.then(function(){
console.log(55555)
什么是Promise?
Promise是抽象异步处理对象以及对其进行各种操作的组件。 其详细内容在接下来我们还会进行介绍,Promise并不是从JavaScript中发祥的概念。
Promise最初被提出是在 E语言中, 它是基于并列/并行处理设计的一种编程语言。
Promise的基本用法
new Promise(function(resolve, reject) { ... })
带有 resolve 、reject两个参数的函数对象。 第一个参数用在处理执行成功的场景,第二个参数则用在处理执行失败的场景。 一旦我们的操作完成即可调用这些函数。
resolve是实例化后Promise用then里面调用的函数
reject是实例化后Promise用catch里面调用的函数
实例化的Promise有3个状态 :等待(pending)、已完成(fulfilled)、已拒绝(rejected)
实例化的Promise的状态,只可能从&等待&转到&完成&态或者&拒绝&态,不能逆向转换,同时&完成&态和&拒绝&态不能相互转换
then和catch方法
then方法负责添加针对已完成的处理函数,同一个实例化后的Promise可以有多个then,then方法默认会返回当前的Promise,也可以自己重新实例化一个新的Promise
then方法对应实例化时候,function的resolve
catch方法负责添加针对拒绝状态下处理函数,同一个实例化后的Promise可以有多个catch,catch方法默认会返回当前的Promise,也可以自己重新实例化一个新的Promise
catch方法对应实例化时候,function的resolve
new Promise(function(resolve,rejected){
var img = new Image();
img.src="/xx.jpg";
//这个会失败
//img.src="/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" //这个会成功
img.onload = function(){
resolve();
img.onerror = function(){
rejected();
.then(function(){
console.log("图片加载成功!")
.catch(function(){
console.log("图片加载失败!")
all和race方法
Promise.all 接收一个 promise对象的数组作为参数,当这个数组里的所有promise对象全部变为resolve或reject状态的时候,它才会去调用 .then 方法
这个比较好用,当有多个请求的时候,一起请求,一起处理
return new Promise(function(resolve,rejected){
setTimeout(function(){
resolve();
Promise.all([setTime(1000),setTime(5000)])
.then(function(){
console.log("5秒后打印!");
只要有一个promise对象进入 FulFilled 或者 Rejected 状态的话,就会继续进行后面的处理。
function setTime(t){
return new Promise(function(resolve,rejected){
setTimeout(function(){
resolve();
Promise.race([setTime(1000),setTime(5000)])
.then(function(){
console.log("1秒后打印!");
一些用法的例子
Promise并非所有的浏览器都支持,比如ie就是不支持,幸好jquery也有类似的用法
1.一个异步里面接着另一个异步
原生Promise的实现
function setTime(t){
return new Promise(function(resolve,reject){
setTimeout(function(){
resolve();
setTime(1000)
.then(function(){
console.log(11111)
return setTime(1000);
.then(function(){
console.log(22222)
return setTime(2000);
.then(function(){
console.log(33333)
return setTime(3000);
.then(function(){
console.log(44444)
return setTime(4000);
.then(function(){
console.log(55555)
jquery的实现
function setTime(t){
return $.Deferred(function(dtd){
setTimeout(function(){
dtd.resolve();
setTime(2000)
.then(function(){
console.log(11111)
return setTime(1000);
.then(function(){
console.log(22222)
return setTime(2000);
.then(function(){
console.log(33333)
return setTime(3000);
.then(function(){
console.log(44444)
return setTime(4000);
.then(function(){
console.log(55555)
2.多个ajax串行的请求(满足后面的请求依赖前面的请求的情况)
原生的实现
chorme浏览器可以直接在博客园下的console直接运行,如果接口都还在的
var urls = {
sideRight : "/aggsite/SideRight",
userStats : "/aggsite/UserStats"
function get(URL){
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', URL, true);
req.onload = function () {
if (req.status === 200) {
resolve(req.responseText);
req.onerror = function () {
req.send();
get(urls.sideRight)
.catch(function(){
console.log("请求失败!!!");
.then(function(html){
console.log(html);
console.log("-----------------------------------------------------------")
return get(urls.userStats);
.catch(function(){
console.log("请求失败!!!");
.then(function(html){
console.log(html)
jquery的实现
var urls = {
sideRight : "/aggsite/SideRight",
userStats : "/aggsite/UserStats"
: urls.sideRight,
dataType : "html"
.fail(function(){
console.log("请求失败!!!");
.done(function(html){
console.log(html)
console.log("-----------------------------------------------------------")
return $.ajax({
: urls.userStats,
dataType : "html"
.fail(function(){
console.log("请求失败!!!");
.done(function(html){
console.log(html)
3.多个请求并行发送(适用于多个请求才能渲染出一个页面的情况)
原生Promise实现
var urls = {
sideRight : "/aggsite/SideRight",
userStats : "/aggsite/UserStats"
function get(URL){
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', URL, true);
req.onload = function () {
if (req.status === 200) {
resolve(req.responseText);
reject(URL);
req.onerror = function () {
reject(URL);
req.send();
Promise.all([get(urls.sideRight),get(urls.userStats)])
.then(function(htmlArr){
console.log(htmlArr[0])
console.log("-----------------------------------------------------------")
console.log(htmlArr[1])
.catch(function(url){
console.log(url+"
: 失败了");
jquery实现
var urls = {
sideRight : "/aggsite/SideRight",
userStats : "/aggsite/UserStats1"
var $ajax1 = $.ajax({
: urls.sideRight,
dataType : "html"
var $ajax2 = $.ajax({
: urls.userStats,
dataType : "html"
$.when($ajax1,$ajax2)
.done(function(response1Arr,response2Arr){
console.log(response1Arr[0]);
console.log("----------------------------");
console.log(response2Arr[1])
.fail(function(res){
console.log("请求失败了")
注:当任意一个请求失败的时候,都会进入fail
阅读(...) 评论()}

我要回帖

更多关于 es6原生promise 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信