JavaScript中的await函數(shù)使用小結(jié)
JavaScript中的await
先來介紹一下async 函數(shù)
async 函數(shù)是使用async關(guān)鍵字聲明的函數(shù)。async 函數(shù)是 AsyncFunction 構(gòu)造函數(shù)的實例,并且其中允許使用 await 關(guān)鍵字。async 和 await 關(guān)鍵字讓我們可以用一種更簡潔的方式寫出基于 Promise 的異步行為,而無需刻意地鏈式調(diào)用 promise。
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
// Expected output: "resolved"
}
asyncCall();
async 函數(shù)可能包含 0 個或者多個 await 表達式。await 表達式會暫停整個 async 函數(shù)的執(zhí)行進程并出讓其控制權(quán),只有當其等待的基于 promise 的異步操作被兌現(xiàn)或被拒絕之后才會恢復進程。promise 的解決值會被當作該 await 表達式的返回值。使用 async/await 關(guān)鍵字就可以在異步代碼中使用普通的 try/catch 代碼塊。
注意: await關(guān)鍵字只在
async 函數(shù)內(nèi)有效。如果你在 async 函數(shù)體之外使用它,就會拋出語法錯誤 SyntaxError 。
備注: async/await的目的為了簡化使用基于 promise 的 API 時所需的語法。async/await 的行為就好像搭配使用了生成器和 promise。
await概念
await 操作符用于等待一個 Promise 兌現(xiàn)并獲取它兌現(xiàn)之后的值。它只能在異步函數(shù)或者模塊頂層中使用。
await expression;
expression為要等待的 Promise 實例,Thenable 對象,或任意類型的值。
返回值:返回從 Promise 實例或 thenable 對象取得的處理結(jié)果。如果等待的值不符合 thenable,則返回表達式本身的值。
異常:拒絕(reject)的原因會被作為異常拋出。
await 通常用于拆開 promise 的包裝,使用方法是傳遞一個 Promise 作為 expression。使用 await 總會暫停當前異步函數(shù)的執(zhí)行,在該 Promise 敲定(settled,指兌現(xiàn)或拒絕)后繼續(xù)執(zhí)行。函數(shù)的執(zhí)行恢復(resume)時,await 表達式的值已經(jīng)變成了 Promise 兌現(xiàn)的值。
若該 Promise 被拒絕(rejected),await 表達式會把拒絕的原因(reason)拋出。當前函數(shù)(await 所在的函數(shù))會出現(xiàn)在拋出的錯誤的棧追蹤(stack trace),否則當前函數(shù)就不會在棧追蹤出現(xiàn)。
await 總會同步地對表達式求值并處理,處理的行為與 Promise.resolve() 一致,不屬于原生 Promise 的值全都會被隱式地轉(zhuǎn)換為 Promise 實例后等待。處理的規(guī)則為,若表達式:
- 是一個原生 Promise(原生Promise 的實例或其派生類的實例,且滿足 expression.constructor ===
- Promise),會被直接用于等待,等待由原生代碼實現(xiàn),該對象的 then() 不會被調(diào)用。
- 是 thenable 對象(包括非原生的 Promise 實例、polyfill、Proxy、派生類等),會構(gòu)造一個新 Promise用于等待,構(gòu)造時會調(diào)用該對象的 then() 方法。
- 不是 thenable 對象,會被包裝進一個已兌現(xiàn)的 Promise 用于等待,其結(jié)果就是表達式的值。
等待 Promise 的兌現(xiàn)
當一個 Promise 被傳遞給 await 操作符,await 將等待該 Promise 兌現(xiàn),并在兌現(xiàn)后返回該 Promise 兌現(xiàn)的值。
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function f1() {
let x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();轉(zhuǎn)換為 promise
若表達式的值不是 Promise,await 會把該值轉(zhuǎn)換為已兌現(xiàn)的 Promise,然后返回其結(jié)果。
async function f3() {
const y = await 20;
console.log(y); // 20
const obj = {};
console.log((await obj) === obj); // true
}
f3();promise 被拒絕
如果 Promise 被拒絕,則拋出拒絕的原因。
async function f4() {
try {
const z = await Promise.reject(30);
} catch (e) {
console.error(e); // 30
}
}
f4();處理被拒絕的 promise
你可以鏈式調(diào)用 catch()(而不是使用 try)以在等待 promise 兌現(xiàn)之前處理被拒絕的 promise。
const response = await promisedFunction().catch((err) => {
console.error(err);
return "default response";
});
// response will be "default response" if the promise is rejected到此這篇關(guān)于JavaScript中的await的文章就介紹到這了,更多相關(guān)JavaScript await內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jquery實現(xiàn)select下拉框美化特效代碼分享
這篇文章主要介紹了jquery實現(xiàn)select下拉框美化特效,實現(xiàn)效果簡潔大方,推薦給大家,有需要的小伙伴可以參考下。2015-08-08

