当前位置:网站首页>async/await 异步函数
async/await 异步函数
2022-07-02 09:43:00 【大海里没有水】
1、async异步函数的使用
// await/async写法
async function foo1() {
}
const foo2 = async () => {
};
2、async异步函数的执行流程

// 都是同步代码,跟普通函数代码没区别
async function foo() {
console.log("内部代码执行1");
console.log("内部代码执行2");
console.log("内部代码执行3");
}
console.log("script start");
foo();
console.log("script end");
3、async异步函数和普通函数的区别1 - 返回值
// 异步函数的返回值一定是个Promise
async function foo() {
console.log("foo start...");
// 1、如果默认不写,默认返回的是undefined
// 2、返回一个值
// return "aaa";
// 3、返回一个thenable
// return {
// then: function (resolve, reject) {
// resolve("haaaaaa");
// },
// };
// 4、返回一个Promise
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hehehehhehe");
}, 2000);
});
}
const promise = foo();
// 这个then是在foo()有return的时候才会被执行,而且是被加到微任务队列里面的
promise.then((res) => {
console.log(res);
});
4、async异步函数和普通函数的区别2 - 异常
async function foo() {
console.log("foo function start~");
console.log("中间代码");
// 异步函数中的异常,会被作为异步函数返回的Promise的reject值的,可以在catch中拿到错误信息
throw new Error("error Message~");
console.log("foo function end");
}
foo().catch((err) => {
console.log('chen', err);
});
console.log("后续还有代码~");
5、async中使用await
// async function foo() {
// await 表达式(Promise)
// }
// 1、await跟上表达式
// function requestData() {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(111);
// }, 1000);
// });
// }
// async function foo() {
// // 什么时候返回res -> 就是requestData()调用resolve的时候
// const res = await requestData();
// // 这些后面的代码,相当于await requestData();中Promise then中执行的。
// console.log('res', res);
// console.log("=============");
// }
// foo();
function requestData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(111);
}, 1000);
});
}
// 2、跟上其他的值
async function foo() {
// 2.1、await 跟上普通值,它会立即返回
// const res1 = await 123;
// 2.2、await跟上对象
// const res1 = await {
// then: function (resolve, reject) {
// resolve("aaaaaaa");
// },
// };
// 2.3、await跟上Promise
// const res1 = await new Promise((resolve) => {
// resolve("bbbb");
// });
// 2.4、reject值,当我们这里面reject的时候,reject的值,会作为整个异步函数foo()的Promise的reject的值,我们需要在外面catch
const res1 = await requestData();
console.log("res", res1);
}
foo().catch((err) => {
console.log('err---', err);
})
边栏推荐
- Initial JDBC programming
- LeetCode—剑指 Offer 59 - I、59 - II
- Sweetheart leader: Wang Xinling
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- Leetcode topic [array] -540- single element in an ordered array
- (C language) octal conversion decimal
- 趣味 面试题
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- Error in kubeadm join: [error port-10250]: port 10250 is in use [error fileavailable--etc kubernetes PKI
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
猜你喜欢

使用Sqoop把ADS层数据导出到MySQL

mysql表的增删改查(进阶)

Embedded Software Engineer career planning

Multiply LCA (nearest common ancestor)

Take you ten days to easily finish the finale of go micro services (distributed transactions)

刷题---二叉树--2

CDA数据分析——AARRR增长模型的介绍、使用

In development, why do you find someone who is paid more than you but doesn't write any code?

CDA data analysis -- Introduction and use of aarrr growth model

Record the range of data that MySQL update will lock
随机推荐
mysql数据库基础
Leetcode122 the best time to buy and sell stocks II
单指令多数据SIMD的SSE/AVX指令集和API
刷题---二叉树--2
堆(优先级队列)
[ybtoj advanced training guide] similar string [string] [simulation]
Openssh remote enumeration username vulnerability (cve-2018-15473)
CDA数据分析——AARRR增长模型的介绍、使用
深拷贝 事件总线
Drools dynamically add, modify, and delete rules
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Find the common ancestor of any two numbers in a binary tree
drools中then部分的写法
Multiply LCA (nearest common ancestor)
Full link voltage measurement
Leetcode122 买卖股票的最佳时机 II
Take you ten days to easily finish the finale of go micro services (distributed transactions)
[FFH] little bear driver calling process (take calling LED light driver as an example)
Sort---
[ybtoj advanced training guidance] judgment overflow [error]