async vue
- IT科技
- 關注:9.55K次
async一般來說是一個加在函式前的修飾符,被async定義的函式通常會預設返回一個Promise物件resolve的值。因此對async函式是能夠直接進行then的,其返回值也就是通過then方法傳入的函式。
說明:
在ES7標準中其實就已經新增了async和await關鍵字,來作為處理非同步請求的一種解決方案,實際上也就是一個語法糖,儘管在ES6中就已經能夠運用生成器語法完成同樣的操作,但是async/await的出現在使用這樣的方式處理非同步請求會更加簡單明瞭。
參考範例:
1、使用async/await語法,示例程式碼如下:
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
var result = await resolveAfter2Seconds();
console.log(result);
// expected output: 'resolved'
}
asyncCall();
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/dianzi/35rrro.html