当前位置:网站首页>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);
})
边栏推荐
- Shutter encapsulated button
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- Openssh remote enumeration username vulnerability (cve-2018-15473)
- Input box assembly of the shutter package
- Drools executes the specified rule
- JZ63 股票的最大利润
- 怎样写一篇赏心悦目的英文数学论文
- Drools dynamically add, modify, and delete rules
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
猜你喜欢

Lekao: 22 year first-class fire engineer "technical practice" knowledge points

甜心教主:王心凌

Go学习笔记—多线程

趣味 面试题

Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)

深拷貝 事件總線

mysql数据库基础

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

MySQL与PostgreSQL抓取慢sql的方法
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers
随机推荐
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
CDH6之Sqoop添加数据库驱动
排序---
IPhone 6 plus is listed in Apple's "retro products" list
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
Go学习笔记—基于Go的进程间通信
Mysql database foundation
Map和Set
Gaode map test case
(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?
Lombok common annotations
Leetcode122 the best time to buy and sell stocks II
Differences between nodes and sharding in ES cluster
ThreadLocal的简单理解
Multiply LCA (nearest common ancestor)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
LeetCode—剑指 Offer 59 - I、59 - II
mysql表的增删改查(进阶)
[ybtoj advanced training guidance] cross the river [BFS]