当前位置:网站首页>Usage and understanding of async/await in JS
Usage and understanding of async/await in JS
2022-06-22 07:42:00 【Huangbao ~】
1、 First of all, we need to understand async and await The basic meaning of
async It's a modifier ,async The defined function will return a by default Promise object resolve Value , So right. async Functions can be used directly then operation , The value returned is then Method
// 0. async Basic usage test
async function fun0() {
console.log(1)
return 1
}
fun0().then( x => { console.log(x) }) // Output results 1, 1,
async function funa() {
console.log('a')
return 'a'
}
funa().then( x => { console.log(x) }) // Output results a, a,
async function funo() {
console.log({})
return {}
}
funo().then( x => { console.log(x) }) // Output results {} {}
async function funp() {
console.log('Promise')
return new Promise(function(resolve, reject){
resolve('Promise')
})
}
funp().then( x => { console.log(x) }) // Output promise promise
await It's also a modifier ,
await keyword Only on the async Internal function , await The role of keywords Is to get Promise Content returned in , What you get is Promise Function resolve perhaps reject Value
// If await It's not a Promise The return value of , It will be processed according to the returned value of the synchronization program

// await keyword Only on the async Internal function , await The role of keywords Is to get Promise Content returned in , What you get is Promise Function resolve perhaps reject Value
// If await It's not a Promise The return value of , It will be processed according to the returned value of the synchronization program , by undefined
const bbb = function(){ return 'string'}
async function funAsy() {
const a = await 1
const b = await new Promise((resolve, reject)=>{
setTimeout(function(){
resolve('time')
}, 3000)
})
const c = await bbb()
console.log(a, b, c)
}
funAsy() // The result of the operation is 3 Seconds later , Output 1, time , string,

// 2. If not used promise The way to do it
function log2(time) {
setTimeout(function(){
console.log(time)
return 1
}, time)
}
async function fun1() {
const a = await log2(5000)
const b = await log2(10000)
const c = log2(2000)
console.log(a)
console.log(1)
}
fun1()
// The above operation result is : Output immediately undefined Output immediately 1 2 Second output 2000 5 Second output 5000 10 Second output 10000
2、 So from this point of view async / await The comprehensive usage of is as follows

// 1. Define one or more common functions , The function must return Promise object , If you return other types of data , It will be handled according to the normal synchronization procedure
function log(time) {
return new Promise((resolve, reject)=> {
setTimeout(function(){
console.log(time)
resolve()
}, time)
})
}
async function fun() {
await log(5000)
await log(10000)
log(1000)
console.log(1)
}
fun()

// 3. async / await An important application of
const asy = function(x, time) {
return new Promise((resolve, reject) =>{
setTimeout(()=>{
resolve(x)
}, time)
})
}
const add = async function() {
const a = await asy(3, 5000)
console.log(a)
const b = await asy(4, 10000)
console.log(b)
const c = await asy(5, 15000)
console.log(a,b,c)
const d = a + b +c
console.log(d)
}
add();
// 5 Second output 3 also 10 Second output 4 also 15 Second output 5 Then output immediately 3,4,5, Then the output 12边栏推荐
- Itemtools permutation
- FileTool
- 网站的日常维护
- AudioQueue
- Open version - inventory description
- Download addresses of Xcode versions
- enable_ irq_ Wake interrupt wakes up the kernel in low power mode
- The applet uses the step bar vant steps in vant
- What are the ways for Taobao merchants to put their babies on the shelves in batches
- 丰田bZ4X取消上市发布会,就算低温充电问题不存在,产品力如何?
猜你喜欢

Canvastotempfilepath of wechat

2 lines of text on the right of the left Avatar

Applet /vant UI to upload files

Chromedriver all versions download

Taobao assistant can not be used. How to export the baby in the warehouse to backup the data package

Get through - coupons

Téléchargement de toutes les versions de chromedriver

Solution to the problem of "brand abuse such as brand inconsistency and stacking in the published product information" prompted by copying and uploading

AutoCAD 2020.3中文版 (旧版本)

目标检测系列——开山之作RCNN原理详解
随机推荐
基于消息传递的并发编程(MPI)之异步收发
Crmeb open version 4.3
Use multithreading to speed up your crawler
Qualcomm platform msm8953 display subsystem learning
Open version - order delivery
The applet uses the step bar vant steps in vant
9、 Textinput component
Query where uiwebview is used in the project
5、 Image component
ASP. Net core development experience
8、 Slider assembly
3、 Stylesheet component
The ranking of websites is very important for websites
网站是否要修改标题
【图论常见模板题】4种最短路解法和2种最小生成树解法
Error e: unable to locate package sudo
easy-rule 初窥
Wechat games (2)
Docker command, docker installation sqlserver2019, docker installation MySQL (continuous update)
Semaphore