当前位置:网站首页>Async/await asynchronous function
Async/await asynchronous function
2022-07-02 12:43:00 【There is no water in the sea】
1、async Use of asynchronous functions
// await/async How to write it
async function foo1() {
}
const foo2 = async () => {
};
2、async The execution flow of asynchronous functions

// It's all synchronous code , It's no different from ordinary function code
async function foo() {
console.log(" Internal code execution 1");
console.log(" Internal code execution 2");
console.log(" Internal code execution 3");
}
console.log("script start");
foo();
console.log("script end");
3、async The difference between asynchronous functions and ordinary functions 1 - Return value
// The return value of an asynchronous function must be Promise
async function foo() {
console.log("foo start...");
// 1、 If you don't write by default , The default return is undefined
// 2、 Returns a value
// return "aaa";
// 3、 Return to one thenable
// return {
// then: function (resolve, reject) {
// resolve("haaaaaa");
// },
// };
// 4、 Return to one Promise
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("hehehehhehe");
}, 2000);
});
}
const promise = foo();
// This then Is in foo() Yes return Only when it's executed , And it is added to the micro task queue
promise.then((res) => {
console.log(res);
});
4、async The difference between asynchronous functions and ordinary functions 2 - abnormal
async function foo() {
console.log("foo function start~");
console.log(" Intermediate code ");
// Exception in asynchronous function , Will be returned as an asynchronous function Promise Of reject It's worth it , Can be in catch Get the error message
throw new Error("error Message~");
console.log("foo function end");
}
foo().catch((err) => {
console.log('chen', err);
});
console.log(" There are codes to follow ~");
5、async Use in await
// async function foo() {
// await expression (Promise)
// }
// 1、await Keep up with the expression
// function requestData() {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(111);
// }, 1000);
// });
// }
// async function foo() {
// // When to return res -> Namely requestData() call resolve When
// const res = await requestData();
// // These later codes , amount to await requestData(); in Promise then Implemented in .
// console.log('res', res);
// console.log("=============");
// }
// foo();
function requestData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(111);
}, 1000);
});
}
// 2、 Keep up with other values
async function foo() {
// 2.1、await Keep up with the normal value , It will return immediately
// const res1 = await 123;
// 2.2、await Keep up with the object
// const res1 = await {
// then: function (resolve, reject) {
// resolve("aaaaaaa");
// },
// };
// 2.3、await keep pace with Promise
// const res1 = await new Promise((resolve) => {
// resolve("bbbb");
// });
// 2.4、reject value , When we are here reject When ,reject Value , As the whole asynchronous function foo() Of Promise Of reject Value , We need to be outside catch
const res1 = await requestData();
console.log("res", res1);
}
foo().catch((err) => {
console.log('err---', err);
})
边栏推荐
- Use sqoop to export ads layer data to MySQL
- Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
- 应用LNK306GN-TL 转换器、非隔离电源
- IPhone 6 plus is listed in Apple's "retro products" list
- bellman-ford AcWing 853. Shortest path with side limit
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- Mui WebView down refresh pull-up load implementation
- 百款拿来就能用的网页特效,不来看看吗?
- Is the neural network (pinn) with embedded physical knowledge a pit?
- [ybtoj advanced training guide] similar string [string] [simulation]
猜你喜欢
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

深拷貝 事件總線

Record the range of data that MySQL update will lock

8 examples of using date commands

Writing method of then part in drools

Dijkstra AcWing 850. Dijkstra finding the shortest circuit II

高性能纠删码编码

The differences and relationships among port, targetport, nodeport and containerport in kubenetes

Deep copy event bus

防抖 节流
随机推荐
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
3 A VTT端接 稳压器 NCP51200MNTXG资料
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
8A 同步降压稳压器 TPS568230RJER_规格信息
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
浏览器存储方案
Oracle从入门到精通(第4版)
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
MySQL and PostgreSQL methods to grab slow SQL
Calculate the maximum path sum of binary tree
一些突然迸发出的程序思想(模块化处理)
bellman-ford AcWing 853. Shortest path with side limit
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
中国交通标志检测数据集
Anti shake throttle
[I'm a mound pytorch tutorial] learning notes
CPU指令集介绍
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)