async awit

作者: Aniugel | 来源:发表于2019-10-06 16:25 被阅读0次

阮一峰ES6 async... await 教程

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 如何通过同步的方式实现异步

相关文章

  • async awit

    阮一峰ES6 async... await 教程 读取文件 fs.readFile1. promise2. ge...

  • async/awit

    async 作为一个关键字放在函数的前面,表示该函数是一个异步函数,意味着该函数的执行不会阻塞后面代码的执行 异步...

  • promise async awit

    Pramise promise 对象异步操作成功之后,将then方法的函数传入作为resolve回调函数调用,第一...

  • javascript异步编程常用方法:promise、async

    1. promise的基本用法 2. 利用promise的链式调用来解决回调问题 3. async awit 写法

  • swift 语法 属性 方法 下标 继承

    swift系列课程 promise 类似于 async awit 异步请求等待的操作,可以避免网络请求一层一层嵌套...

  • async

    async/await特点 async/await更加语义化,async是“异步”的简写,async functi...

  • ES7 - async

    async 基本使用 async 传递参数 async 错误处理

  • async和await

    浅谈Async/Await用 async/await 来处理异步 async和await async:声明一个异步...

  • promise async settimeout setImme

    // setTimeout async promise执行顺序总结async function async1() ...

  • async和await的使用

    async函数 什么是async函数? async函数是使用async关键字声明的函数。 mdn文档:https:...

网友评论

    本文标题:async awit

    本文链接:https://www.haomeiwen.com/subject/phrupctx.html