代码之家  ›  专栏  ›  技术社区  ›  chobo2

多个异步调用?

  •  -1
  • chobo2  · 技术社区  · 7 年前

    我正在使用async/await。我想知道如何并行执行多个异步调用?

    是吗

    async method(){
       call1();
       call2();
    }
    

    至少在调试器中,它一次调用一个。

    我不确定,因为我使用的是mobx状态树“flow”特性,所以它是否会被阻塞 call2 call1 完成了。

    call1: flow(function*() {
        const response = yield axios.post()
    }),
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Shubham Verma    7 年前

    async.js 并行方法。它还将减少处理不同呼叫数据的负担。它也会这样做:

    async.parallel([
        //different async calls you can add as many you want
        function(callback) {
            setTimeout(function() {
                callback(null, 'one');
            }, 200);
        },
        function(callback) {
            setTimeout(function() {
                callback(null, 'two');
            }, 100);
        }
    ],
    // optional callback
    function(err, results) {
        // the results array will equal ['one','two'] even though
        // the second function had a shorter timeout.
    });
    
        2
  •  0
  •   jayarjo    7 年前

    Promise.all :

    async method() {
       return await Promise.all([
           call1()
           call2()
       ])
    }