Prepending(進(jìn)行時(shí)),Resolve(成功了),Reject(失敗了),then......等
1.Promise 的含義
Promise 是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函數(shù)和事件——更合理和更強(qiáng)大。它由社區(qū)最早提出和實(shí)現(xiàn),ES6 將其寫進(jìn)了語(yǔ)言標(biāo)準(zhǔn),統(tǒng)一了用法,原生提供了Promise對(duì)象。
所謂Promise,簡(jiǎn)單說就是一個(gè)容器,里面保存著某個(gè)未來才會(huì)結(jié)束的事件(通常是一個(gè)異步操作)的結(jié)果。從語(yǔ)法上說,Promise 是一個(gè)對(duì)象,從它可以獲取異步操作的消息。Promise 提供統(tǒng)一的 API,各種異步操作都可以用同樣的方法進(jìn)行處理。
Promise對(duì)象有以下兩個(gè)特點(diǎn)。
(1)對(duì)象的狀態(tài)不受外界影響。Promise對(duì)象代表一個(gè)異步操作,有三種狀態(tài):pending(進(jìn)行中)、fulfilled(已成功)和rejected(已失?。?。只有異步操作的結(jié)果,可以決定當(dāng)前是哪一種狀態(tài),任何其他操作都無法改變這個(gè)狀態(tài)。這也是Promise這個(gè)名字的由來,它的英語(yǔ)意思就是“承諾”,表示其他手段無法改變。
(2)一旦狀態(tài)改變,就不會(huì)再變,任何時(shí)候都可以得到這個(gè)結(jié)果。Promise對(duì)象的狀態(tài)改變,只有兩種可能:從pending變?yōu)?code>fulfilled和從pending變?yōu)?code>rejected。只要這兩種情況發(fā)生,狀態(tài)就凝固了,不會(huì)再變了,會(huì)一直保持這個(gè)結(jié)果,這時(shí)就稱為 resolved(已定型)。如果改變已經(jīng)發(fā)生了,你再對(duì)Promise對(duì)象添加回調(diào)函數(shù),也會(huì)立即得到這個(gè)結(jié)果。這與事件(Event)完全不同,事件的特點(diǎn)是,如果你錯(cuò)過了它,再去監(jiān)聽,是得不到結(jié)果的。
注意,為了行文方便,本章后面的resolved統(tǒng)一只指fulfilled狀態(tài),不包含rejected狀態(tài)。
有了Promise對(duì)象,就可以將異步操作以同步操作的流程表達(dá)出來,避免了層層嵌套的回調(diào)函數(shù)。此外,Promise對(duì)象提供統(tǒng)一的接口,使得控制異步操作更加容易。
Promise也有一些缺點(diǎn)。首先,無法取消Promise,一旦新建它就會(huì)立即執(zhí)行,無法中途取消。其次,如果不設(shè)置回調(diào)函數(shù),Promise內(nèi)部拋出的錯(cuò)誤,不會(huì)反應(yīng)到外部。第三,當(dāng)處于pending狀態(tài)時(shí),無法得知目前進(jìn)展到哪一個(gè)階段(剛剛開始還是即將完成)。
如果某些事件不斷地反復(fù)發(fā)生,一般來說,使用 Stream 模式是比部署Promise更好的選擇。
2.基本用法
ES6 規(guī)定,Promise對(duì)象是一個(gè)構(gòu)造函數(shù),用來生成Promise實(shí)例。
下面代碼創(chuàng)造了一個(gè)Promise實(shí)例。
const promise = new Promise(function(resolve, reject) {
// ... some code
if (/* 異步操作成功 */){
resolve(value);
} else {
reject(error);
}
});
Promise構(gòu)造函數(shù)接受一個(gè)函數(shù)作為參數(shù),該函數(shù)的兩個(gè)參數(shù)分別是resolve和reject。它們是兩個(gè)函數(shù),由 JavaScript 引擎提供,不用自己部署。
resolve函數(shù)的作用是,將Promise對(duì)象的狀態(tài)從“未完成”變?yōu)椤俺晒Α保磸?pending 變?yōu)?resolved),在異步操作成功時(shí)調(diào)用,并將異步操作的結(jié)果,作為參數(shù)傳遞出去;reject函數(shù)的作用是,將Promise對(duì)象的狀態(tài)從“未完成”變?yōu)椤笆 保磸?pending 變?yōu)?rejected),在異步操作失敗時(shí)調(diào)用,并將異步操作報(bào)出的錯(cuò)誤,作為參數(shù)傳遞出去。
Promise實(shí)例生成以后,可以用then方法分別指定resolved狀態(tài)和rejected狀態(tài)的回調(diào)函數(shù)。
promise.then(function(value) {
// success
}, function(error) {
// failure
});
then方法可以接受兩個(gè)回調(diào)函數(shù)作為參數(shù)。第一個(gè)回調(diào)函數(shù)是Promise對(duì)象的狀態(tài)變?yōu)?code>resolved時(shí)調(diào)用,第二個(gè)回調(diào)函數(shù)是Promise對(duì)象的狀態(tài)變?yōu)?code>rejected時(shí)調(diào)用。其中,第二個(gè)函數(shù)是可選的,不一定要提供。這兩個(gè)函數(shù)都接受Promise對(duì)象傳出的值作為參數(shù)。
下面是一個(gè)Promise對(duì)象的簡(jiǎn)單例子。
function timeout(ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms, 'done');
});
}
timeout(100).then((value) => {
console.log(value);
});
上面代碼中,timeout方法返回一個(gè)Promise實(shí)例,表示一段時(shí)間以后才會(huì)發(fā)生的結(jié)果。過了指定的時(shí)間(ms參數(shù))以后,Promise實(shí)例的狀態(tài)變?yōu)?code>resolved,就會(huì)觸發(fā)then方法綁定的回調(diào)函數(shù)。
Promise 新建后就會(huì)立即執(zhí)行。
let promise = new Promise(function(resolve, reject) {
console.log('Promise');
resolve();
});
promise.then(function() {
console.log('resolved.');
});
console.log('Hi!');
// Promise
// Hi!
// resolved
上面代碼中,Promise 新建后立即執(zhí)行,所以首先輸出的是Promise。然后,then方法指定的回調(diào)函數(shù),將在當(dāng)前腳本所有同步任務(wù)執(zhí)行完才會(huì)執(zhí)行,所以resolved最后輸出。
下面是異步加載圖片的例子。
function loadImageAsync(url) {
return new Promise(function(resolve, reject) {
const image = new Image();
image.onload = function() {
resolve(image);
};
image.onerror = function() {
reject(new Error('Could not load image at ' + url));
};
image.src = url;
});
}
上面代碼中,使用Promise包裝了一個(gè)圖片加載的異步操作。如果加載成功,就調(diào)用resolve方法,否則就調(diào)用reject方法。
下面是一個(gè)用Promise對(duì)象實(shí)現(xiàn)的 Ajax 操作的例子。
const getJSON = function(url) {
const promise = new Promise(function(resolve, reject){
const handler = function() {
if (this.readyState !== 4) {
return;
}
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
};
const client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();
});
return promise;
};
getJSON("/posts.json").then(function(json) {
console.log('Contents: ' + json);
}, function(error) {
console.error('出錯(cuò)了', error);
});
上面代碼中,getJSON是對(duì) XMLHttpRequest 對(duì)象的封裝,用于發(fā)出一個(gè)針對(duì) JSON 數(shù)據(jù)的 HTTP 請(qǐng)求,并且返回一個(gè)Promise對(duì)象。需要注意的是,在getJSON內(nèi)部,resolve函數(shù)和reject函數(shù)調(diào)用時(shí),都帶有參數(shù)。
如果調(diào)用resolve函數(shù)和reject函數(shù)時(shí)帶有參數(shù),那么它們的參數(shù)會(huì)被傳遞給回調(diào)函數(shù)。reject函數(shù)的參數(shù)通常是Error對(duì)象的實(shí)例,表示拋出的錯(cuò)誤;resolve函數(shù)的參數(shù)除了正常的值以外,還可能是另一個(gè) Promise 實(shí)例,比如像下面這樣。
const p1 = new Promise(function (resolve, reject) {
// ...
});
const p2 = new Promise(function (resolve, reject) {
// ...
resolve(p1);
})
上面代碼中,p1和p2都是 Promise 的實(shí)例,但是p2的resolve方法將p1作為參數(shù),即一個(gè)異步操作的結(jié)果是返回另一個(gè)異步操作。
注意,這時(shí)p1的狀態(tài)就會(huì)傳遞給p2,也就是說,p1的狀態(tài)決定了p2的狀態(tài)。如果p1的狀態(tài)是pending,那么p2的回調(diào)函數(shù)就會(huì)等待p1的狀態(tài)改變;如果p1的狀態(tài)已經(jīng)是resolved或者rejected,那么p2的回調(diào)函數(shù)將會(huì)立刻執(zhí)行。
const p1 = new Promise(function (resolve, reject) {
setTimeout(() => reject(new Error('fail')), 3000)
})
const p2 = new Promise(function (resolve, reject) {
setTimeout(() => resolve(p1), 1000)
})
p2
.then(result => console.log(result))
.catch(error => console.log(error))
// Error: fail
上面代碼中,p1是一個(gè) Promise,3 秒之后變?yōu)?code>rejected。p2的狀態(tài)在 1 秒之后改變,resolve方法返回的是p1。由于p2返回的是另一個(gè) Promise,導(dǎo)致p2自己的狀態(tài)無效了,由p1的狀態(tài)決定p2的狀態(tài)。所以,后面的then語(yǔ)句都變成針對(duì)后者(p1)。又過了 2 秒,p1變?yōu)?code>rejected,導(dǎo)致觸發(fā)catch方法指定的回調(diào)函數(shù)。
注意,調(diào)用resolve或reject并不會(huì)終結(jié) Promise 的參數(shù)函數(shù)的執(zhí)行。
new Promise((resolve, reject) => {
resolve(1);
console.log(2);
}).then(r => {
console.log(r);
});
// 2
// 1
上面代碼中,調(diào)用resolve(1)以后,后面的console.log(2)還是會(huì)執(zhí)行,并且會(huì)首先打印出來。這是因?yàn)榱⒓?resolved 的 Promise 是在本輪事件循環(huán)的末尾執(zhí)行,總是晚于本輪循環(huán)的同步任務(wù)。
一般來說,調(diào)用resolve或reject以后,Promise 的使命就完成了,后繼操作應(yīng)該放到then方法里面,而不應(yīng)該直接寫在resolve或reject的后面。所以,最好在它們前面加上return語(yǔ)句,這樣就不會(huì)有意外。
new Promise((resolve, reject) => {
return resolve(1);
// 后面的語(yǔ)句不會(huì)執(zhí)行
console.log(2);
})
3.Promise.prototype.then()
Promise 實(shí)例具有then方法,也就是說,then方法是定義在原型對(duì)象Promise.prototype上的。它的作用是為 Promise 實(shí)例添加狀態(tài)改變時(shí)的回調(diào)函數(shù)。前面說過,then方法的第一個(gè)參數(shù)是resolved狀態(tài)的回調(diào)函數(shù),第二個(gè)參數(shù)(可選)是rejected狀態(tài)的回調(diào)函數(shù)。
then方法返回的是一個(gè)新的Promise實(shí)例(注意,不是原來那個(gè)Promise實(shí)例)。因此可以采用鏈?zhǔn)綄懛?,?code>then方法后面再調(diào)用另一個(gè)then方法。
getJSON("/posts.json").then(function(json) {
return json.post;
}).then(function(post) {
// ...
});
上面的代碼使用then方法,依次指定了兩個(gè)回調(diào)函數(shù)。第一個(gè)回調(diào)函數(shù)完成以后,會(huì)將返回結(jié)果作為參數(shù),傳入第二個(gè)回調(diào)函數(shù)。
采用鏈?zhǔn)降?code>then,可以指定一組按照次序調(diào)用的回調(diào)函數(shù)。這時(shí),前一個(gè)回調(diào)函數(shù),有可能返回的還是一個(gè)Promise對(duì)象(即有異步操作),這時(shí)后一個(gè)回調(diào)函數(shù),就會(huì)等待該Promise對(duì)象的狀態(tài)發(fā)生變化,才會(huì)被調(diào)用。
getJSON("/post/1.json").then(function(post) {
return getJSON(post.commentURL);
}).then(function funcA(comments) {
console.log("resolved: ", comments);
}, function funcB(err){
console.log("rejected: ", err);
});
上面代碼中,第一個(gè)then方法指定的回調(diào)函數(shù),返回的是另一個(gè)Promise對(duì)象。這時(shí),第二個(gè)then方法指定的回調(diào)函數(shù),就會(huì)等待這個(gè)新的Promise對(duì)象狀態(tài)發(fā)生變化。如果變?yōu)?code>resolved,就調(diào)用funcA,如果狀態(tài)變?yōu)?code>rejected,就調(diào)用funcB。
如果采用箭頭函數(shù),上面的代碼可以寫得更簡(jiǎn)潔。
getJSON("/post/1.json").then(
post => getJSON(post.commentURL)
).then(
comments => console.log("resolved: ", comments),
err => console.log("rejected: ", err)
);
4.Promise.prototype.catch()
Promise.prototype.catch方法是.then(null, rejection)的別名,用于指定發(fā)生錯(cuò)誤時(shí)的回調(diào)函數(shù)。
getJSON('/posts.json').then(function(posts) {
// ...
}).catch(function(error) {
// 處理 getJSON 和 前一個(gè)回調(diào)函數(shù)運(yùn)行時(shí)發(fā)生的錯(cuò)誤
console.log('發(fā)生錯(cuò)誤!', error);
});
上面代碼中,getJSON方法返回一個(gè) Promise 對(duì)象,如果該對(duì)象狀態(tài)變?yōu)?code>resolved,則會(huì)調(diào)用then方法指定的回調(diào)函數(shù);如果異步操作拋出錯(cuò)誤,狀態(tài)就會(huì)變?yōu)?code>rejected,就會(huì)調(diào)用catch方法指定的回調(diào)函數(shù),處理這個(gè)錯(cuò)誤。另外,then方法指定的回調(diào)函數(shù),如果運(yùn)行中拋出錯(cuò)誤,也會(huì)被catch方法捕獲。
p.then((val) => console.log('fulfilled:', val))
.catch((err) => console.log('rejected', err));
// 等同于
p.then((val) => console.log('fulfilled:', val))
.then(null, (err) => console.log("rejected:", err));
下面是一個(gè)例子。
const promise = new Promise(function(resolve, reject) {
throw new Error('test');
});
promise.catch(function(error) {
console.log(error);
});
// Error: test
上面代碼中,promise拋出一個(gè)錯(cuò)誤,就被catch方法指定的回調(diào)函數(shù)捕獲。注意,上面的寫法與下面兩種寫法是等價(jià)的。
// 寫法一
const promise = new Promise(function(resolve, reject) {
try {
throw new Error('test');
} catch(e) {
reject(e);
}
});
promise.catch(function(error) {
console.log(error);
});
// 寫法二
const promise = new Promise(function(resolve, reject) {
reject(new Error('test'));
});
promise.catch(function(error) {
console.log(error);
});
比較上面兩種寫法,可以發(fā)現(xiàn)reject方法的作用,等同于拋出錯(cuò)誤。
如果 Promise 狀態(tài)已經(jīng)變成resolved,再拋出錯(cuò)誤是無效的。
const promise = new Promise(function(resolve, reject) {
resolve('ok');
throw new Error('test');
});
promise
.then(function(value) { console.log(value) })
.catch(function(error) { console.log(error) });
// ok
上面代碼中,Promise 在resolve語(yǔ)句后面,再拋出錯(cuò)誤,不會(huì)被捕獲,等于沒有拋出。因?yàn)?Promise 的狀態(tài)一旦改變,就永久保持該狀態(tài),不會(huì)再變了。
Promise 對(duì)象的錯(cuò)誤具有“冒泡”性質(zhì),會(huì)一直向后傳遞,直到被捕獲為止。也就是說,錯(cuò)誤總是會(huì)被下一個(gè)catch語(yǔ)句捕獲。
getJSON('/post/1.json').then(function(post) {
return getJSON(post.commentURL);
}).then(function(comments) {
// some code
}).catch(function(error) {
// 處理前面三個(gè)Promise產(chǎn)生的錯(cuò)誤
});
上面代碼中,一共有三個(gè) Promise 對(duì)象:一個(gè)由getJSON產(chǎn)生,兩個(gè)由then產(chǎn)生。它們之中任何一個(gè)拋出的錯(cuò)誤,都會(huì)被最后一個(gè)catch捕獲。
一般來說,不要在then方法里面定義 Reject 狀態(tài)的回調(diào)函數(shù)(即then的第二個(gè)參數(shù)),總是使用catch方法。
// bad
promise
.then(function(data) {
// success
}, function(err) {
// error
});
// good
promise
.then(function(data) { //cb
// success
})
.catch(function(err) {
// error
});
上面代碼中,第二種寫法要好于第一種寫法,理由是第二種寫法可以捕獲前面then方法執(zhí)行中的錯(cuò)誤,也更接近同步的寫法(try/catch)。因此,建議總是使用catch方法,而不使用then方法的第二個(gè)參數(shù)。
跟傳統(tǒng)的try/catch代碼塊不同的是,如果沒有使用catch方法指定錯(cuò)誤處理的回調(diào)函數(shù),Promise 對(duì)象拋出的錯(cuò)誤不會(huì)傳遞到外層代碼,即不會(huì)有任何反應(yīng)。
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下面一行會(huì)報(bào)錯(cuò),因?yàn)閤沒有聲明
resolve(x + 2);
});
};
someAsyncThing().then(function() {
console.log('everything is great');
});
setTimeout(() => { console.log(123) }, 2000);
// Uncaught (in promise) ReferenceError: x is not defined
// 123
上面代碼中,someAsyncThing函數(shù)產(chǎn)生的 Promise 對(duì)象,內(nèi)部有語(yǔ)法錯(cuò)誤。瀏覽器運(yùn)行到這一行,會(huì)打印出錯(cuò)誤提示ReferenceError: x is not defined,但是不會(huì)退出進(jìn)程、終止腳本執(zhí)行,2 秒之后還是會(huì)輸出123。這就是說,Promise 內(nèi)部的錯(cuò)誤不會(huì)影響到 Promise 外部的代碼,通俗的說法就是“Promise 會(huì)吃掉錯(cuò)誤”。
這個(gè)腳本放在服務(wù)器執(zhí)行,退出碼就是0(即表示執(zhí)行成功)。不過,Node 有一個(gè)unhandledRejection事件,專門監(jiān)聽未捕獲的reject錯(cuò)誤,上面的腳本會(huì)觸發(fā)這個(gè)事件的監(jiān)聽函數(shù),可以在監(jiān)聽函數(shù)里面拋出錯(cuò)誤。
process.on('unhandledRejection', function (err, p) {
throw err;
});
上面代碼中,unhandledRejection事件的監(jiān)聽函數(shù)有兩個(gè)參數(shù),第一個(gè)是錯(cuò)誤對(duì)象,第二個(gè)是報(bào)錯(cuò)的 Promise 實(shí)例,它可以用來了解發(fā)生錯(cuò)誤的環(huán)境信息。
注意,Node 有計(jì)劃在未來廢除unhandledRejection事件。如果 Promise 內(nèi)部有未捕獲的錯(cuò)誤,會(huì)直接終止進(jìn)程,并且進(jìn)程的退出碼不為 0。
再看下面的例子。
const promise = new Promise(function (resolve, reject) {
resolve('ok');
setTimeout(function () { throw new Error('test') }, 0)
});
promise.then(function (value) { console.log(value) });
// ok
// Uncaught Error: test
上面代碼中,Promise 指定在下一輪“事件循環(huán)”再拋出錯(cuò)誤。到了那個(gè)時(shí)候,Promise 的運(yùn)行已經(jīng)結(jié)束了,所以這個(gè)錯(cuò)誤是在 Promise 函數(shù)體外拋出的,會(huì)冒泡到最外層,成了未捕獲的錯(cuò)誤。
一般總是建議,Promise 對(duì)象后面要跟catch方法,這樣可以處理 Promise 內(nèi)部發(fā)生的錯(cuò)誤。catch方法返回的還是一個(gè) Promise 對(duì)象,因此后面還可以接著調(diào)用then方法。
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下面一行會(huì)報(bào)錯(cuò),因?yàn)閤沒有聲明
resolve(x + 2);
});
};
someAsyncThing()
.catch(function(error) {
console.log('oh no', error);
})
.then(function() {
console.log('carry on');
});
// oh no [ReferenceError: x is not defined]
// carry on
上面代碼運(yùn)行完catch方法指定的回調(diào)函數(shù),會(huì)接著運(yùn)行后面那個(gè)then方法指定的回調(diào)函數(shù)。如果沒有報(bào)錯(cuò),則會(huì)跳過catch方法。
修正一下這個(gè)位置說的是上一個(gè)catch
Promise.resolve()
.catch(function(error) {
console.log('oh no', error);
})
.then(function() {
console.log('carry on');
});
// carry on
上面的代碼因?yàn)闆]有報(bào)錯(cuò),跳過了catch方法,直接執(zhí)行后面的then方法。此時(shí),要是then方法里面報(bào)錯(cuò),就與前面的catch無關(guān)了。
catch方法之中,還能再拋出錯(cuò)誤。
const someAsyncThing = function() {
return new Promise(function(resolve, reject) {
// 下面一行會(huì)報(bào)錯(cuò),因?yàn)閤沒有聲明
resolve(x + 2);
});
};
someAsyncThing().then(function() {
return someOtherAsyncThing();
}).catch(function(error) {
console.log('oh no', error);
// 下面一行會(huì)報(bào)錯(cuò),因?yàn)?y 沒有聲明
y + 2;
}).then(function() {
console.log('carry on');
});
// oh no [ReferenceError: x is not defined]
上面代碼中,catch方法拋出一個(gè)錯(cuò)誤,因?yàn)楹竺鏇]有別的catch方法了,導(dǎo)致這個(gè)錯(cuò)誤不會(huì)被捕獲,也不會(huì)傳遞到外層。如果改寫一下,結(jié)果就不一樣了。
someAsyncThing().then(function() {
return someOtherAsyncThing();
}).catch(function(error) {
console.log('oh no', error);
// 下面一行會(huì)報(bào)錯(cuò),因?yàn)閥沒有聲明
y + 2;
}).catch(function(error) {
console.log('carry on', error);
});
// oh no [ReferenceError: x is not defined]
// carry on [ReferenceError: y is not defined]
上面代碼中,第二個(gè)catch方法用來捕獲前一個(gè)catch方法拋出的錯(cuò)誤。
5.Promise.prototype.finally()
finally方法用于指定不管 Promise 對(duì)象最后狀態(tài)如何,都會(huì)執(zhí)行的操作。該方法是 ES2018 引入標(biāo)準(zhǔn)的。
promise
.then(result => {···})
.catch(error => {···})
.finally(() => {···});
上面代碼中,不管promise最后的狀態(tài),在執(zhí)行完then或catch指定的回調(diào)函數(shù)以后,都會(huì)執(zhí)行finally方法指定的回調(diào)函數(shù)。
下面是一個(gè)例子,服務(wù)器使用 Promise 處理請(qǐng)求,然后使用finally方法關(guān)掉服務(wù)器。
server.listen(port)
.then(function () {
// ...
})
.finally(server.stop);
finally方法的回調(diào)函數(shù)不接受任何參數(shù),這意味著沒有辦法知道,前面的 Promise 狀態(tài)到底是fulfilled還是rejected。這表明,finally方法里面的操作,應(yīng)該是與狀態(tài)無關(guān)的,不依賴于 Promise 的執(zhí)行結(jié)果。
finally本質(zhì)上是then方法的特例。
promise
.finally(() => {
// 語(yǔ)句
});
// 等同于
promise
.then(
result => {
// 語(yǔ)句
return result;
},
error => {
// 語(yǔ)句
throw error;
}
);
上面代碼中,如果不使用finally方法,同樣的語(yǔ)句需要為成功和失敗兩種情況各寫一次。有了finally方法,則只需要寫一次。
它的實(shí)現(xiàn)也很簡(jiǎn)單。
Promise.prototype.finally = function (callback) {
let P = this.constructor;
return this.then(
value => P.resolve(callback()).then(() => value),
reason => P.resolve(callback()).then(() => { throw reason })
);
};
上面代碼中,不管前面的 Promise 是fulfilled還是rejected,都會(huì)執(zhí)行回調(diào)函數(shù)callback。
從上面的實(shí)現(xiàn)還可以看到,finally方法總是會(huì)返回原來的值。
// resolve 的值是 undefined
Promise.resolve(2).then(() => {}, () => {})
// resolve 的值是 2
Promise.resolve(2).finally(() => {})
// reject 的值是 undefined
Promise.reject(3).then(() => {}, () => {})
// reject 的值是 3
Promise.reject(3).finally(() => {})
6.Promise.all()
Promise.all方法用于將多個(gè) Promise 實(shí)例,包裝成一個(gè)新的 Promise 實(shí)例。
const p = Promise.all([p1, p2, p3]);
上面代碼中,Promise.all方法接受一個(gè)數(shù)組作為參數(shù),p1、p2、p3都是 Promise 實(shí)例,如果不是,就會(huì)先調(diào)用下面講到的Promise.resolve方法,將參數(shù)轉(zhuǎn)為 Promise 實(shí)例,再進(jìn)一步處理。(Promise.all方法的參數(shù)可以不是數(shù)組,但必須具有 Iterator 接口,且返回的每個(gè)成員都是 Promise 實(shí)例。)
p的狀態(tài)由p1、p2、p3決定,分成兩種情況。
(1)只有p1、p2、p3的狀態(tài)都變成fulfilled,p的狀態(tài)才會(huì)變成fulfilled,此時(shí)p1、p2、p3的返回值組成一個(gè)數(shù)組,傳遞給p的回調(diào)函數(shù)。
(2)只要p1、p2、p3之中有一個(gè)被rejected,p的狀態(tài)就變成rejected,此時(shí)第一個(gè)被reject的實(shí)例的返回值,會(huì)傳遞給p的回調(diào)函數(shù)。
下面是一個(gè)具體的例子。
// 生成一個(gè)Promise對(duì)象的數(shù)組
const promises = [2, 3, 5, 7, 11, 13].map(function (id) {
return getJSON('/post/' + id + ".json");
});
Promise.all(promises).then(function (posts) {
// ...
}).catch(function(reason){
// ...
});
上面代碼中,promises是包含 6 個(gè) Promise 實(shí)例的數(shù)組,只有這 6 個(gè)實(shí)例的狀態(tài)都變成fulfilled,或者其中有一個(gè)變?yōu)?code>rejected,才會(huì)調(diào)用Promise.all方法后面的回調(diào)函數(shù)。
下面是另一個(gè)例子。
const databasePromise = connectDatabase();
const booksPromise = databasePromise
.then(findAllBooks);
const userPromise = databasePromise
.then(getCurrentUser);
Promise.all([
booksPromise,
userPromise
])
.then(([books, user]) => pickTopRecommentations(books, user));
上面代碼中,booksPromise和userPromise是兩個(gè)異步操作,只有等到它們的結(jié)果都返回了,才會(huì)觸發(fā)pickTopRecommentations這個(gè)回調(diào)函數(shù)。
注意,如果作為參數(shù)的 Promise 實(shí)例,自己定義了catch方法,那么它一旦被rejected,并不會(huì)觸發(fā)Promise.all()的catch方法。
const p1 = new Promise((resolve, reject) => {
resolve('hello');
})
.then(result => result)
.catch(e => e);
const p2 = new Promise((resolve, reject) => {
throw new Error('報(bào)錯(cuò)了');
})
.then(result => result)
.catch(e => e);
Promise.all([p1, p2])
.then(result => console.log(result))
.catch(e => console.log(e));
// ["hello", Error: 報(bào)錯(cuò)了]
上面代碼中,p1會(huì)resolved,p2首先會(huì)rejected,但是p2有自己的catch方法,該方法返回的是一個(gè)新的 Promise 實(shí)例,p2指向的實(shí)際上是這個(gè)實(shí)例。該實(shí)例執(zhí)行完catch方法后,也會(huì)變成resolved,導(dǎo)致Promise.all()方法參數(shù)里面的兩個(gè)實(shí)例都會(huì)resolved,因此會(huì)調(diào)用then方法指定的回調(diào)函數(shù),而不會(huì)調(diào)用catch方法指定的回調(diào)函數(shù)。
如果p2沒有自己的catch方法,就會(huì)調(diào)用Promise.all()的catch方法。
const p1 = new Promise((resolve, reject) => {
resolve('hello');
})
.then(result => result);
const p2 = new Promise((resolve, reject) => {
throw new Error('報(bào)錯(cuò)了');
})
.then(result => result);
Promise.all([p1, p2])
.then(result => console.log(result))
.catch(e => console.log(e));
// Error: 報(bào)錯(cuò)了
7.Promise.race()
Promise.race方法同樣是將多個(gè) Promise 實(shí)例,包裝成一個(gè)新的 Promise 實(shí)例。
const p = Promise.race([p1, p2, p3]);
上面代碼中,只要p1、p2、p3之中有一個(gè)實(shí)例率先改變狀態(tài),p的狀態(tài)就跟著改變。那個(gè)率先改變的 Promise 實(shí)例的返回值,就傳遞給p的回調(diào)函數(shù)。
Promise.race方法的參數(shù)與Promise.all方法一樣,如果不是 Promise 實(shí)例,就會(huì)先調(diào)用下面講到的Promise.resolve方法,將參數(shù)轉(zhuǎn)為 Promise 實(shí)例,再進(jìn)一步處理。
下面是一個(gè)例子,如果指定時(shí)間內(nèi)沒有獲得結(jié)果,就將 Promise 的狀態(tài)變?yōu)?code>reject,否則變?yōu)?code>resolve。
const p = Promise.race([
fetch('/resource-that-may-take-a-while'),
new Promise(function (resolve, reject) {
setTimeout(() => reject(new Error('request timeout')), 5000)
})
]);
p
.then(console.log)
.catch(console.error);
上面代碼中,如果 5 秒之內(nèi)fetch方法無法返回結(jié)果,變量p的狀態(tài)就會(huì)變?yōu)?code>rejected,從而觸發(fā)catch方法指定的回調(diào)函數(shù)。
8.Promise.resolve()
有時(shí)需要將現(xiàn)有對(duì)象轉(zhuǎn)為 Promise 對(duì)象,Promise.resolve方法就起到這個(gè)作用。
const jsPromise = Promise.resolve($.ajax('/whatever.json'));
上面代碼將 jQuery 生成的deferred對(duì)象,轉(zhuǎn)為一個(gè)新的 Promise 對(duì)象。
Promise.resolve等價(jià)于下面的寫法。
Promise.resolve('foo')
// 等價(jià)于
new Promise(resolve => resolve('foo'))
Promise.resolve方法的參數(shù)分成四種情況。
(1)參數(shù)是一個(gè) Promise 實(shí)例
如果參數(shù)是 Promise 實(shí)例,那么Promise.resolve將不做任何修改、原封不動(dòng)地返回這個(gè)實(shí)例。
(2)參數(shù)是一個(gè)thenable對(duì)象
thenable對(duì)象指的是具有then方法的對(duì)象,比如下面這個(gè)對(duì)象。
let thenable = {
then: function(resolve, reject) {
resolve(42);
}
};
Promise.resolve方法會(huì)將這個(gè)對(duì)象轉(zhuǎn)為 Promise 對(duì)象,然后就立即執(zhí)行thenable對(duì)象的then方法。
let thenable = {
then: function(resolve, reject) {
resolve(42);
}
};
let p1 = Promise.resolve(thenable);
p1.then(function(value) {
console.log(value); // 42
});
上面代碼中,thenable對(duì)象的then方法執(zhí)行后,對(duì)象p1的狀態(tài)就變?yōu)?code>resolved,從而立即執(zhí)行最后那個(gè)then方法指定的回調(diào)函數(shù),輸出 42。
(3)參數(shù)不是具有then方法的對(duì)象,或根本就不是對(duì)象
如果參數(shù)是一個(gè)原始值,或者是一個(gè)不具有then方法的對(duì)象,則Promise.resolve方法返回一個(gè)新的 Promise 對(duì)象,狀態(tài)為resolved。
const p = Promise.resolve('Hello');
p.then(function (s){
console.log(s)
});
// Hello
上面代碼生成一個(gè)新的 Promise 對(duì)象的實(shí)例p。由于字符串Hello不屬于異步操作(判斷方法是字符串對(duì)象不具有 then 方法),返回 Promise 實(shí)例的狀態(tài)從一生成就是resolved,所以回調(diào)函數(shù)會(huì)立即執(zhí)行。Promise.resolve方法的參數(shù),會(huì)同時(shí)傳給回調(diào)函數(shù)。
(4)不帶有任何參數(shù)
Promise.resolve方法允許調(diào)用時(shí)不帶參數(shù),直接返回一個(gè)resolved狀態(tài)的 Promise 對(duì)象。
所以,如果希望得到一個(gè) Promise 對(duì)象,比較方便的方法就是直接調(diào)用Promise.resolve方法。
const p = Promise.resolve();
p.then(function () {
// ...
});
上面代碼的變量p就是一個(gè) Promise 對(duì)象。
需要注意的是,立即resolve的 Promise 對(duì)象,是在本輪“事件循環(huán)”(event loop)的結(jié)束時(shí),而不是在下一輪“事件循環(huán)”的開始時(shí)。
setTimeout(function () {
console.log('three');
}, 0);
Promise.resolve().then(function () {
console.log('two');
});
console.log('one');
// one
// two
// three
上面代碼中,setTimeout(fn, 0)在下一輪“事件循環(huán)”開始時(shí)執(zhí)行,Promise.resolve()在本輪“事件循環(huán)”結(jié)束時(shí)執(zhí)行,console.log('one')則是立即執(zhí)行,因此最先輸出。
9.Promise.reject()
Promise.reject(reason)方法也會(huì)返回一個(gè)新的 Promise 實(shí)例,該實(shí)例的狀態(tài)為rejected。
const p = Promise.reject('出錯(cuò)了');
// 等同于
const p = new Promise((resolve, reject) => reject('出錯(cuò)了'))
p.then(null, function (s) {
console.log(s)
});
// 出錯(cuò)了
上面代碼生成一個(gè) Promise 對(duì)象的實(shí)例p,狀態(tài)為rejected,回調(diào)函數(shù)會(huì)立即執(zhí)行。
注意,Promise.reject()方法的參數(shù),會(huì)原封不動(dòng)地作為reject的理由,變成后續(xù)方法的參數(shù)。這一點(diǎn)與Promise.resolve方法不一致。
const thenable = {
then(resolve, reject) {
reject('出錯(cuò)了');
}
};
Promise.reject(thenable)
.catch(e => {
console.log(e === thenable)
})
// true
上面代碼中,Promise.reject方法的參數(shù)是一個(gè)thenable對(duì)象,執(zhí)行以后,后面catch方法的參數(shù)不是reject拋出的“出錯(cuò)了”這個(gè)字符串,而是thenable對(duì)象。
10應(yīng)用
加載圖片
我們可以將圖片的加載寫成一個(gè)Promise,一旦加載完成,Promise的狀態(tài)就發(fā)生變化。
const preloadImage = function (path) {
return new Promise(function (resolve, reject) {
const image = new Image();
image.onload = resolve;
image.onerror = reject;
image.src = path;
});
};
Generator 函數(shù)與 Promise 的結(jié)合
使用 Generator 函數(shù)管理流程,遇到異步操作的時(shí)候,通常返回一個(gè)Promise對(duì)象。
function getFoo () {
return new Promise(function (resolve, reject){
resolve('foo');
});
}
const g = function* () {
try {
const foo = yield getFoo();
console.log(foo);
} catch (e) {
console.log(e);
}
};
function run (generator) {
const it = generator();
function go(result) {
if (result.done) return result.value;
return result.value.then(function (value) {
return go(it.next(value));
}, function (error) {
return go(it.throw(error));
});
}
go(it.next());
}
run(g);
上面代碼的 Generator 函數(shù)g之中,有一個(gè)異步操作getFoo,它返回的就是一個(gè)Promise對(duì)象。函數(shù)run用來處理這個(gè)Promise對(duì)象,并調(diào)用下一個(gè)next方法。
11.Promise.try()
實(shí)際開發(fā)中,經(jīng)常遇到一種情況:不知道或者不想?yún)^(qū)分,函數(shù)f是同步函數(shù)還是異步操作,但是想用 Promise 來處理它。因?yàn)檫@樣就可以不管f是否包含異步操作,都用then方法指定下一步流程,用catch方法處理f拋出的錯(cuò)誤。一般就會(huì)采用下面的寫法。
Promise.resolve().then(f)
上面的寫法有一個(gè)缺點(diǎn),就是如果f是同步函數(shù),那么它會(huì)在本輪事件循環(huán)的末尾執(zhí)行。
const f = () => console.log('now');
Promise.resolve().then(f);
console.log('next');
// next
// now
上面代碼中,函數(shù)f是同步的,但是用 Promise 包裝了以后,就變成異步執(zhí)行了。
那么有沒有一種方法,讓同步函數(shù)同步執(zhí)行,異步函數(shù)異步執(zhí)行,并且讓它們具有統(tǒng)一的 API 呢?回答是可以的,并且還有兩種寫法。第一種寫法是用async函數(shù)來寫。
const f = () => console.log('now');
(async () => f())();
console.log('next');
// now
// next
上面代碼中,第二行是一個(gè)立即執(zhí)行的匿名函數(shù),會(huì)立即執(zhí)行里面的async函數(shù),因此如果f是同步的,就會(huì)得到同步的結(jié)果;如果f是異步的,就可以用then指定下一步,就像下面的寫法。
(async () => f())()
.then(...)
需要注意的是,async () => f()會(huì)吃掉f()拋出的錯(cuò)誤。所以,如果想捕獲錯(cuò)誤,要使用promise.catch方法。
(async () => f())()
.then(...)
.catch(...)
第二種寫法是使用new Promise()。
const f = () => console.log('now');
(
() => new Promise(
resolve => resolve(f())
)
)();
console.log('next');
// now
// next
上面代碼也是使用立即執(zhí)行的匿名函數(shù),執(zhí)行new Promise()。這種情況下,同步函數(shù)也是同步執(zhí)行的。
鑒于這是一個(gè)很常見的需求,所以現(xiàn)在有一個(gè)提案,提供Promise.try方法替代上面的寫法。
const f = () => console.log('now');
Promise.try(f);
console.log('next');
// now
// next
事實(shí)上,Promise.try存在已久,Promise 庫(kù)Bluebird、Q和when,早就提供了這個(gè)方法。
由于Promise.try為所有操作提供了統(tǒng)一的處理機(jī)制,所以如果想用then方法管理流程,最好都用Promise.try包裝一下。這樣有許多好處,其中一點(diǎn)就是可以更好地管理異常。
function getUsername(userId) {
return database.users.get({id: userId})
.then(function(user) {
return user.name;
});
}
上面代碼中,database.users.get()返回一個(gè) Promise 對(duì)象,如果拋出異步錯(cuò)誤,可以用catch方法捕獲,就像下面這樣寫。
database.users.get({id: userId})
.then(...)
.catch(...)
但是database.users.get()可能還會(huì)拋出同步錯(cuò)誤(比如數(shù)據(jù)庫(kù)連接錯(cuò)誤,具體要看實(shí)現(xiàn)方法),這時(shí)你就不得不用try...catch去捕獲。
try {
database.users.get({id: userId})
.then(...)
.catch(...)
} catch (e) {
// ...
}
上面這樣的寫法就很笨拙了,這時(shí)就可以統(tǒng)一用promise.catch()捕獲所有同步和異步的錯(cuò)誤。
Promise.try(database.users.get({id: userId}))
.then(...)
.catch(...)
事實(shí)上,Promise.try就是模擬try代碼塊,就像promise.catch模擬的是catch代碼塊。
資料來源->阮一峰