当前位置:网站首页>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);
})
边栏推荐
- Drools dynamically add, modify, and delete rules
- Go学习笔记—基于Go的进程间通信
- Sweetheart leader: Wang Xinling
- [C language] Yang Hui triangle, customize the number of lines of the triangle
- 趣味 面试题
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
- [ybtoj advanced training guide] similar string [string] [simulation]
- Lombok common annotations
猜你喜欢
Find the common ancestor of any two numbers in a binary tree
[FFH] little bear driver calling process (take calling LED light driver as an example)
mysql索引和事务
Use sqoop to export ads layer data to MySQL
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Heap (priority queue)
Addition, deletion, modification and query of MySQL table (Advanced)
初始JDBC 编程
记录一下MySql update会锁定哪些范围的数据
Multiply LCA (nearest common ancestor)
随机推荐
倍增 LCA(最近公共祖先)
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Leetcode739 daily temperature
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Find the common ancestor of any two numbers in a binary tree
(C language) octal conversion decimal
刷题---二叉树--2
Input box assembly of the shutter package
Leetcode - Sword finger offer 51 Reverse pairs in an array
Shuttle encapsulated AppBar
Introduction to CPU instruction set
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
LeetCode—剑指 Offer 37、38
Go learning notes - multithreading
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
[C language] convert decimal numbers to binary numbers
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
Docker-compose配置Mysql,Redis,MongoDB
堆(优先级队列)