当前位置:网站首页>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);
})
边栏推荐
猜你喜欢

arcgis js 4.x 地图中加入图片

Lekao.com: experience sharing of junior economists and previous candidates in customs clearance

Find the common ancestor of any two numbers in a binary tree

From scratch, develop a web office suite (3): mouse events

(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.

Is the neural network (pinn) with embedded physical knowledge a pit?

mysql数据库基础

MySQL indexes and transactions

ThreadLocal的简单理解

CDA数据分析——AARRR增长模型的介绍、使用
随机推荐
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
使用Sqoop把ADS层数据导出到MySQL
Jenkins user rights management
Go learning notes - multithreading
Sweetheart leader: Wang Xinling
Leetcode122 the best time to buy and sell stocks II
(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?
Distributed machine learning framework and high-dimensional real-time recommendation system
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
(C language) octal conversion decimal
Go学习笔记—基于Go的进程间通信
arcgis js 4.x 地图中加入图片
SSH automatically disconnects (pretends to be dead) after a period of no operation
Leetcode922 sort array by parity II
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Deep Copy Event bus
中国交通标志检测数据集
浏览器存储方案
[FFH] little bear driver calling process (take calling LED light driver as an example)