-1

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

我做吗

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

至少从调试器看来,它一次调用一个。

我不确定是否因为我正在使用 mobx 状态树“流”功能,这是否可能会阻止call2发生直到call1完成。

call1: flow(function*() {
    const response = yield axios.post()
}),
4

2 回答 2

0

使用Promise.all

async method() {
   return await Promise.all([
       call1()
       call2()
   ])
}
于 2019-05-24T08:21:35.100 回答
0

您可以尝试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.
});
于 2019-05-19T06:17:57.210 回答