当前位置:网站首页>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);
})
边栏推荐
- [FFH] little bear driver calling process (take calling LED light driver as an example)
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- 染色法判定二分图 AcWing 860. 染色法判定二分图
- Floyd AcWing 854. Floyd求最短路
- Execute any method of any class through reflection
- JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
- Is the neural network (pinn) with embedded physical knowledge a pit?
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- 一些突然迸发出的程序思想(模块化处理)
- JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
猜你喜欢
Simple use of drools decision table
深拷貝 事件總線
spfa AcWing 851. SPFA finding the shortest path
Redis sentinel mechanism and configuration
Execute any method of any class through reflection
线性DP AcWing 897. 最长公共子序列
Heap acwing 839 Simulated reactor
C#修饰符
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
spfa AcWing 852. spfa判断负环
随机推荐
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
[ybtoj advanced training guidance] judgment overflow [error]
Introduction to CPU instruction set
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
Some sudden program ideas (modular processing)
Enhance network security of kubernetes with cilium
Bom Dom
Adding database driver to sqoop of cdh6
哈希表 AcWing 841. 字符串哈希
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Go learning notes - multithreading
Anti shake throttle
Is the neural network (pinn) with embedded physical knowledge a pit?
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
What data types does redis have and their application scenarios
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
Intel internal instructions - AVX and avx2 learning notes