About async/await you should take it seriously

Deep understanding of async/await


async function


A syntactic sugar is that asynchronous operations are simpler


return value The return value is a promise object

The return value is the value when the promise is resolved
The value of Throw is the reason when the Promise is rejected



async function test() {
return true
}
const p = test()
console.log(p) // print out a promise, the status is resolved, the value is true

// Promise {: true}
// [[Prototype]]: Promise
// [[PromiseState]]: "fulfilled"
// [[PromiseResult]]: true

p.then((data) => {
console.log(data) // true
})

async function test() {
throw new Error('error')
}
const p = test()
console.log(p) // print out a promise, the status is rejected, and the value is error
p.then((data) => {
console.log(data) //The reason of the printed promise is error
})

It can be seen that the return value of the async function is a promise
await function

Can only appear inside or the outermost layer of an async function
wait for the value of a promise object
The status of the promise of await is rejected, and subsequent execution is interrupted

await can await promise and non-promsie, if non-primse, for example: await 1 returns 1

await to wait for the state of the promise to be resolved
async function async1() {
console.log('async1 start')
await async2() // await is the state of waiting for promise, and then get the value
console.log('async1 end')
}
async function async2() {
return Promsie.resolve().then(_ => {
console.log('async2 promise')
})
}
async1()
/*
print result
async1 start
async2 promise
async1 end
*/

await is waiting for the state of the promise to be rejected
async function f() {
await Promise.reject('error')
// subsequent code will not be executed
console.log(1)
await 100
}

// Solution 1
async function f() {
await Promise.reject('error').catch(err => {
// exception handling
})
console.log(1)
await 100
}

// Solution 2
async function f() {
try {
await Promise.reject('error')
} catch (e) {
// exception handling
} finally {
}
console.log(1)
await 100
}

The principle of async function implementation
Implementation principle: Generator + automatic executor
async functions are syntactic sugar for Generators and Promises
application
Read file with async function scheme
const fs = require('fs')

async function readFilesByAsync() {
const files = [
'/Users/xxx/Desktop/Web/1.json',
'/Users/xxx/Desktop/Web/2.json',
'/Users/xxx/Desktop/Web/3.json'
]
const readFile = function(src) {
return new Promise((resolve, reject) => {
fs.readFile(src, (err, data) => {
if(err) reject(err)
resolve(data)
})
})
}

const str0 = await readFile(files[0])
console.log(str0.toString())
const str1 = await readFile(files[1])
console.log(str1.toString())
const str2 = await readFile(files[2])
console.log(str2.toString())
}

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00