当前位置:网站首页>async 函数
async 函数
2022-04-22 10:27:00 【进击的小白兔vl】
async 函数和 Generator 函数
async 函数就是 Generator 函数的语法糖
async函数就是将 Generator 函数的星号(*)替换成async,将yield替换成await
以下四点改进
(1)内置执行器
Generator 函数的执行必须靠执行器,所以才有了co模块,而async函数自带执行器
(2)更好的语义。
async表示函数里有异步操作,await表示紧跟在后面的表达式需要等待结果
(3)更广的适用性。
co模块约定,yield命令后面只能是 Thunk 函数或 Promise 对象,而async函数的await命令后面,可以是 Promise 对象和原始类型的值
(4)返回值是 Promise
async函数的返回值是 Promise 对象
async函数完全可以看作多个异步操作,包装成的一个 Promise 对象,而await命令就是内部then命令的语法糖。
基本用法
async函数返回一个 Promise 对象,可以使用then方法添加回调函数。当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句
async 函数有多种使用形式
// 函数声明
async function foo() {
}
// 函数表达式
const foo = async function () {
};
// 对象的方法
let obj = {
async foo() {
} };
obj.foo().then(...)
// Class 的方法
class Storage {
constructor() {
this.cachePromise = caches.open('avatars');
}
async getAvatar(name) {
const cache = await this.cachePromise;
return cache.match(`/avatars/${
name}.jpg`);
}
}
const storage = new Storage();
storage.getAvatar('jake').then(…);
// 箭头函数
const foo = async () => {
};
语法
函数返回一个 Promise 对象。
async函数内部return语句返回的值,会成为then方法回调函数的参数。
async function f() {
return 'hello world';
}
f().then(v => console.log(v))
// "hello world"
async函数内部抛出错误,会导致返回的 Promise 对象变为reject状态。抛出的错误对象会被catch方法回调函数接收到
async function f() {
throw new Error('出错了');
}
f().then(
v => console.log('resolve', v),
e => console.log('reject', e)
)
//reject Error: 出错了
Promise 对象的状态变化
async函数返回的 Promise 对象,必须等到内部所有await命令后面的 Promise 对象执行完,才会发生状态改变,除非遇到return语句或者抛出错误。也就是说,只有async函数内部的异步操作执行完,才会执行then方法指定的回调函数。
await 命令
正常情况下,await命令后面是一个 Promise 对象,返回该对象的结果。如果不是 Promise 对象,就直接返回对应的值
await命令后面是一个thenable对象(即定义了then方法的对象),那么await会将其等同于 Promise 对象。
await命令后面的 Promise 对象如果变为reject状态,则reject的参数会被catch方法的回调函数接收到。
任何一个await语句后面的 Promise 对象变为reject状态,那么整个async函数都会中断执行
错误处理
如果await后面的异步操作出错,那么等同于async函数返回的 Promise 对象被reject
async function f() {
await new Promise(function (resolve, reject) {
throw new Error('出错了');
});
}
f()
.then(v => console.log(v))
.catch(e => console.log(e))
// Error:出错了
防止出错的方法,也是将其放在try…catch代码块之中。
async function f() {
try {
await new Promise(function (resolve, reject) {
throw new Error('出错了');
});
} catch(e) {
}
return await('hello world');
}
注意:
- await命令后面的Promise对象,运行结果可能是rejected,所以最好把await命令放在try…catch代码块中。
- 多个await命令后面的异步操作,如果不存在继发关系,最好让它们同时触发。
- await命令只能用在async函数之中,如果用在普通函数,就会报错
- async 函数可以保留运行堆栈
async 函数的实现原理
就是将 Generator 函数和自动执行器,包装在一个函数里
async function fn(args) {
// ...
}
// 等同于
function fn(args) {
return spawn(function* () {
// ...
});
}
spawn函数就是自动执行器
版权声明
本文为[进击的小白兔vl]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_46404694/article/details/124316692
边栏推荐
- Analysis of image lazy loading (three implementation methods and two optimization methods)
- [FAQ] how do HMS core push services and local creation notification messages overlap each other?
- 【无标题】
- QByteArray的一些函数转换使用
- 【307期】从实现原理来讲,Nacos 为什么这么强?
- TIANTI 22 analog l3-2 puzzle a punch in reward (30 points)
- 【设计必用!】网页设计常用色彩搭配表 《配色表》
- *CTF 2022 Reverse Writeup
- 基于容器的PaaS混合云的几种形式
- Best practices for cost reduction and efficiency increase of public cloud
猜你喜欢

机器人系统设计-coppeliasim仿真

Laya Uncaught ReferenceError: spine is not defined

自学编程千万别再傻傻地读理论书,程序员:这都是别人玩剩下的

004-MYSQL的查询过程

01背包问题——以及变形问题

5. Embedded controller (EC) learns PS / 2 protocol

Share some unknown and super easy-to-use API functions in sklearn module

Query process of 004-mysql

OneFlow學習筆記:從Functor到OpExprInterpreter

数字化时代,企业运维面临现状及挑战分析解读
随机推荐
golang 时间格式化
Analysis of image lazy loading (three implementation methods and two optimization methods)
Formdate save data
Creating student management system with MySQL
eval说明
MySql5.7.26安装
oracle中時間函數總結
【无标题】
cesium地图右击菜单(cesium篇.72)
Addition, deletion, modification and query of advanced MySQL data (DML)
Has2022 | the 19th Huawei global analyst conference is about to start!
Here comes the article ~ share compressed and decompressed files [online website]
下一代web服务器Caddy —— 筑梦之路
点云配准(一)— ICP方法
Slim 2022 Outlook: cram istio's complexity into the smart black box
Golang time formatting
《日志:每个软件工程师都应该了解实时数据的统一抽象》
Laya Uncaught ReferenceError: spine is not defined
Query process of 004-mysql
公有云降本增效最佳实践