• node.js async/await 继发执行与并发执行


    async/await 继发执行与并发执行,看如何控制

    两个异步函数 foo bar

    function foo() {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('foo:' + new Date().toLocaleString())
                resolve('foo')
            }, 2000)
        })
    }
    
    function bar() {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('bar:' + new Date().toLocaleString())
                resolve('bar')
            }, 2000)
        })
    }

    继发执行

    //继发执行
    async function main() {
        console.log('beginTime:' + new Date().toLocaleString())
        var foostr = await foo();
        console.log(new Date().toLocaleString())
        console.log(foostr);
        var barstr = await bar();
        console.log(new Date().toLocaleString())
        console.log(barstr);
        console.log('endTime:' + new Date().toLocaleString())
    }
    
    
    //继发执行
    async function main2() {
        let docs = [foo, bar];
        console.log('beginTime:' + new Date().toLocaleString())
        for (let doc of docs) {
            var str = await doc();
            console.log(new Date().toLocaleString())
            console.log(str);
        }
        console.log('endTime:' + new Date().toLocaleString())
    }

    并发执行

    //并发执行 
    async function async_main() {
        console.log('beginTime:' + new Date().toLocaleString())
        let [get_foo, get_bar] = await Promise.all([foo(), bar()]);
        console.log(new Date().toLocaleString());
        console.log(get_foo);
        console.log(get_bar);
        console.log('endTime:' + new Date().toLocaleString())
    }
    
    
    //并发执行 
    async function async_main4() {
        let docs = [foo(), bar()];
        console.log('beginTime:' + new Date().toLocaleString())
        for (let doc of docs) {
            var str = await doc;
            console.log(new Date().toLocaleString())
            console.log(str);
        }
        console.log('endTime:' + new Date().toLocaleString())
    
    }
    
    
    //并发执行
    async function async_main2() {
        console.log('beginTime:' + new Date().toLocaleString())
        let fooPromise = foo();
        let barPromise = bar();
        let get_foo = await fooPromise;
        console.log(new Date().toLocaleString());
        console.log(get_foo);
        let get_bar = await barPromise;
        console.log(new Date().toLocaleString());
        console.log(get_bar);
        console.log('endTime:' + new Date().toLocaleString())
    }
    
    //并发执行 但是 会先执行 endTime
    async function async_main3() {
        let docs = [foo, bar];
        console.log('beginTime:' + new Date().toLocaleString())
        docs.forEach(async (val) => {
            var str = await val();
            console.log(new Date().toLocaleString())
            console.log(str);
        })
        console.log('endTime:' + new Date().toLocaleString())
    }
  • 相关阅读:
    webstorm 2017 激活破解 最新 2018
    phpexcel 导出xsl乱码
    微信小程序的z-index在苹果ios无效
    onenote架设在局域网服务器
    .gitignore忽略多层文件夹用**
    phpstorm 使用xdebug断点
    Phpstudy 无法启动mysql
    git使用kdiff3合并乱码问题
    小程序回退刷新操作
    Navicat 连接远程服务器mysql 长时间不操作会连接很久
  • 原文地址:https://www.cnblogs.com/xbblogs/p/8946912.html
Copyright © 2020-2023  润新知