async/await

  1. 它保证函数的返回值为 promise。
  2. 用更少的.then()块来封装代码,同时它看起来很像同步代码

注意:可能会因为大量await的promises相继发生而变慢。

async关键字

使用 async 关键字,把它放在函数声明之前,使其成为 async function。

let hello = async function() {
return "Hello"
};
//let hello = async () => { return "Hello" };
hello(); //Promise { <state>: "fulfilled", <value>: "Hello" }

await关键字

await 只在异步函数里面才起作用。

promise & async/await

//promise
fetch('coffee.jpg').then(response => response.blob())
.then(myBlob => {
let image = document.createElement('img');
image.src = URL.createObjectURL(myBlob);
document.body.appendChild(image);
}).catch(e => {
console.log('an error with fetch img: ' + e.message);
});
//async/await
async function fetchImg(){
try{
let myFetch = await fetch('coffee.jpg');
let myBlob = await myFetch.blob();
let image = document.createElement('img');
image.src = URL.createObjectURL(myBlob);
document.body.appendChild(image);
}catch(e){
console.log('an error with fetch img: ' + e);
}
}
//promise + async/await
(async function fetchImg(){
let myFetch = await fetch('coffee.jpg');
return await myFetch.blob();
})().then(blob=>{
let image = document.createElement('img');
image.src = URL.createObjectURL(myBlob);
document.body.appendChild(image);
}).catch(e => {
console.log('an error with fetch img: ' + e.message);
});

async/await 的缺点

await 关键字会阻塞其后的代码,直到promise完成,就像执行同步操作一样。

这意味着您的代码可能会因为大量awaitpromises相继发生而变慢。每个await都会等待前一个完成,而你实际想要的是所有的这些promises同时开始处理(就像我们没有使用async/await时那样)。

有一种模式可以缓解这个问题——通过将 Promise 对象存储在变量中来同时开始它们,然后等待它们全部执行完毕。

function timeoutPromise(time) {
return new Promise((resolve, reject) => {
setTimeout(()=>{
resolve("done");
}, time);
});
}; let startTime = Date.now();
timeTest().then(() => {
let timeTaken = Date.now() - startTime;
alert("Time taken in milliseconds: " + timeTaken);
}) //slow:大约是9s
async function timeTest() {
await timeoutPromise(3000);
await timeoutPromise(3000);
await timeoutPromise(3000);
}
//fast:大约是3s
async function timeTest() {
const timeoutPromise1 = timeoutPromise(3000),timeoutPromise2 = timeoutPromise(3000),timeoutPromise3 = timeoutPromise(3000);
await timeoutPromise1;
await timeoutPromise2;
await timeoutPromise3;
}
//or:大约是3s
await Promise.all([timeoutPromise(3000),timeoutPromise(3000),timeoutPromise(3000)]);

旧式异步回调的缺点

  • 嵌套回调可能很麻烦且难以阅读(即“回调地狱”)
  • 每层嵌套都需要故障回调,而使用promises,您只需使用一个.catch()代码块来处理整个链的错误。
  • 异步回调不是很优雅。
  • Promise回调总是按照它们放在事件队列中的严格顺序调用;异步回调不是。
  • 当传入到一个第三方库时,异步回调对函数如何执行失去完全控制。

最新文章

  1. StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)
  2. 自动化测试工具QTP的使用实例 分类: 软件测试 2015-06-17 00:23 185人阅读 评论(0) 收藏
  3. svg学习(四)circle
  4. struct内存对齐1:gcc与VC的差别
  5. Proxy模式:管理第三方API
  6. Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
  7. 反射,得到Type引用的三种方式
  8. ASP.NET MVC 5 学习教程:添加控制器
  9. Android之淘宝商品列表长按遮罩效果
  10. mint17上建立lamp环境
  11. Maven 项目中的 pom.xml 文件内容说明
  12. .net下web页生产一维条形码
  13. vim 的编辑模式 命令模式
  14. 34.scrapy解决爬虫翻页问题
  15. STM32 F4 General-purpose Timers for Periodic Interrupts
  16. Windows正向绑定shell和反向反弹shell的Python代码
  17. Java之IO(十三)File、Filter、Piped、String和InputStreamReader与OutputStreamWriter
  18. springboot自定义异常页面
  19. MySQL性能调优与架构设计——第3章 MySQL存储引擎简介
  20. 独木舟(51NOD 1432 )

热门文章

  1. 2022-05-12内部群每日三题-清辉PMP
  2. Java Development Kit下载地址
  3. React.CreateContext
  4. jmeter接口自动化-读取CSV文件执行测试用例
  5. npm install报错C:\Users\Guyang\AppData\Roaming\npm-cache\_logs\xxx-14T01_06_33_159Z-debug-0.log
  6. unity3D mirror网络游戏开发笔记
  7. ADE-XL bsub提交超时
  8. Task :app:lintVitalRelease FAILED
  9. Install MySQL wsl1
  10. user define language in vscode