当前位置:网站首页>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);
})
边栏推荐
- 线性DP AcWing 899. 编辑距离
- 8 examples of using date commands
- 堆 AcWing 839. 模拟堆
- 正确遍历EntryList方法
- js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
- "As a junior college student, I found out how difficult it is to counter attack after graduation."
- Find the common ancestor of any two numbers in a binary tree
- js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
- Drools terminates the execution of other rules after executing one rule
- Drools executes string rules or executes a rule file
猜你喜欢

Adding database driver to sqoop of cdh6

堆 AcWing 838. 堆排序

JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

应用LNK306GN-TL 转换器、非隔离电源

Distributed machine learning framework and high-dimensional real-time recommendation system

China traffic sign detection data set

"As a junior college student, I found out how difficult it is to counter attack after graduation."

Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand

js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
随机推荐
Visual studio efficient and practical extension tools and plug-ins
线性DP AcWing 895. 最长上升子序列
Hash table acwing 840 Simulated hash table
Fluent fluent library encapsulation
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
Drools dynamically add, modify, and delete rules
防抖 节流
Redis avalanche, penetration, breakdown
JSON序列化 与 解析
About asp Net MVC project in local vs running response time is too long to access, the solution!
H5 to app
3 A VTT端接 稳压器 NCP51200MNTXG资料
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Deep Copy Event bus
js3day(数组操作,js冒泡排序,函数,调试窗口,作用域及作用域链,匿名函数,对象,Math对象)
Drools executes the specified rule
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Dijkstra AcWing 850. Dijkstra求最短路 II
高性能纠删码编码