async awit
2019-10-06 本文已影响0人
Aniugel
ES2017,规定 async
nodeJs
读取文件 fs.readFile
1. promise
2. genrator
3. async
--------------------------------------
async function fn(){ //表示异步,这个函数里面有异步任务
let result = await xxx //表示后面结果需要等待
}
--------------------------------------
async特点:
1. await只能放到async函数中
2. 相比genrator语义化更强
3. await后面可以是promise对象,也可以数字、字符串、布尔
4. async函数返回是一个promise对象
5. 只要await语句后面Promise状态变成 reject, 那么整个async函数会中断执行
--------------------------------------
如何解决async函数中抛出错误,影响后续代码:
a).
try{
}catch(e){
}
b). promise本身catch
--------------------------------------
个人建议大家:
try{
let f1 = await readFile('data/a.txt');
let f3 = await readFile('data/c.txt');
let f2 = await readFile('data/b.txt');
}catch(e){}
--------------------------------------
读取文件 fs.readFile
1. promise
2. genrator
3. async
// 目录结构
// --data
// ----a.txt
// ----b.txt
// ----c.txt
// --promise.js 根目录
// promise.js
const fs = require('fs')
const readFile = function (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if (err) reject(err);
resolve(data)
})
})
}
// promise
// readFile('data/a.txt').then(res => {
// console.log(res.toString());
// return readFile('data/b.txt')
// }).then(res => {
// console.log(res.toString())
// return readFile('data/c.txt')
// }).then(res => {
// console.log(res.toString())
// })
// generator
// function* gen () {
// yield readFile('data/a.txt')
// yield readFile('data/b.txt')
// yield readFile('data/c.txt')
// }
// let g1 = gen()
// g1.next().value.then(res => {
// console.log(res.toString());
// return g1.next().value
// }).then(res => {
// console.log(res.toString());
// return g1.next().value
// }).then(res => {
// console.log(res.toString());
// })
// async
async function fn () {
// let f1 = await readFile('data/a.txt');
// console.log(f1.toString())
// let f2 = await readFile('data/b.txt');
// console.log(f2.toString())
// let f3 = await readFile('data/c.txt');
// console.log(f3.toString())
let [a, b, c] = await Promise.all([
readFile('data/a.txt'),
readFile('data/b.txt'),
readFile('data/c.txt'),
])
console.log(a.toString())
console.log(b.toString())
console.log(c.toString())
}
fn()
async function fn(){ //表示异步,这个函数里面有异步任务
let result = await xxx //表示后面结果需要等待
}
async特点:
1. await只能放到async函数中
2. 相比genrator语义化更强
3. await后面可以是promise对象,也可以数字、字符串、布尔
4. async函数返回是一个promise对象
5. 只要await语句后面Promise状态变成 reject, 那么整个async函数会中断执行
// async function fn() {
// // console.log('async fn()')
// // return 'welcome'
// throw new Error('出错了')
// }
// fn().then(res => {
// console.log(res)
// }, err => {
// console.log(err)
// })
async function fn() {
// 只要await语句后面Promise状态变成 reject,
// 那么整个async函数会中断执行
// let a = await Promise.reject('出问题了')
let a = await Promise.resolve('success')
console.log('async fn()', a)
return '成功'
}
fn().then(res => {
console.log('res', res)
}).catch(err => {
console.log('err', err)
})
如何解决async函数中抛出错误,影响后续代码:
a).
try{
}catch(e){
}
b). promise本身catch
// async function fn() {
// try {
// await Promise.reject('出问题了')
// } catch (e) {
// let a = await Promise.resolve('success')
// console.log('---', a)
// }
// }
// fn().then(res => {
// console.log('res', res)
// }).catch(err => {
// console.log('err', err)
// })
async function fn() {
await Promise.reject('出问题了').catch(err => {
console.log(err)
})
let a = await Promise.resolve('success')
console.log('---', a)
}
fn().then(res => {
console.log('res', res)
})
(头条、微医)Async/Await 如何通过同步的方式实现异步