簡體   English   中英

如何為一組相互依賴的任務構建異步等待代碼?

[英]How to construct async-await code for a set of tasks which are dependent on one another?

我正在將 async.auto 流轉換為異步等待代碼。 為了簡化我的問題,說我有一組任務和這些任務相互依賴的列表,我如何將其轉換為異步等待代碼。 這是我認為將涵蓋所有情況的示例(如果沒有,請糾正我)。

任務集 - a(), b(), c(), f(), d(resultOfA, resultOfB), e(resultOfB, resultOfC), g(resultOfF, resultOfE) 這里要執行 d 我們需要從 a 返回的值和 b,為了執行 e,我們需要 b 和 c,對於 g,我們需要 e 和 f。

請注意,我想盡快完成所有任務

編輯:添加我需要轉換的示例異步自動流

async.auto({
    a: function(callback) {
        setTimeout(() => {
            let resA = 3;
            callback(null, resA);
        }, 1000);
    },
    b: function(callback) {
        setTimeout(() => {
            let resB = 4;
            callback(null, resB);
        }, 1000);
    },
    c: function(callback) {
        setTimeout(() => {
            let resC = 5;
            callback(null, resC);
        }, 1000);
    },
    d: ['a', 'b', function(results, callback) {
        setTimeout(() => {
            //following line represents some computations which depends on resA and resB
            resD = results.a + results.b;
            callback(null, resD);
        }, 1000);
    }],
    e: ['b', 'c', function(results, callback) {
        setTimeout(() => {
            resE = results.b + results.c;
            callback(null, resE);
        }, 1000);
    }],
    f: function(callback) {
        callback(null, resF);
    },
    g: ['e', 'f', function(results, callback) {
        setTimeout(() => {
            resG = results.e + results.f;
            callback(null, resG);
        }, 1000);
    }]
}, function(err, results) {
    if (err) {
        console.log('err : ', err);
    }
    console.log('results : ', results);
});

我知道如何從這三個問題並行和串行運行任務 -

  1. 使用 async/await 並行運行任務的方法
  2. 從並行運行的任務中獲取結果的方法
  3. 使用 Promise.all() 和 async await 並行運行任務的比較

Promises 只完成一次。

async function foo() {
  a = some_promise_factory();
  b = some_promise_factory();
  c = some_promise_factory();
  f = some_promise_factory();
  d = ab(a, b) // the promise resulting from ab is created
               // *before* that from bc, as it is assigned to d
  e = bc(b, c) // doesn't matter if promise b is used again
  g = ef(e, f) // an 'await promise_e' in ef won't resolve
               // until e is resolved, which won't resolve until
               // bc resolves, which won't resolve until
               // both 'await promise_b' and 'await promise_c' ..

  // can await any promise in an async function..
  return await g;
}

然后在abbcef ,例如。 里面的 await 與ab(await a, await b) 如果先將 await 應用於參數,則在解決這些參數之前不會進行調用。 選擇最適合給定任務的表單。

async function ab (promise_a, promise_b) {
  return (await promise_a) + (await promise_b) + 1;
}

async function bc (promise_b, promise_c) {
  return (await promise_b) + (await promise_c) * 2;
}

async function ef (promise_e, promise_f) {
  return (await promise_e) + (await promise_f) / 3;
}

雖然async函數隱式地返回一個 Promise,但一個普通的函數返回一個 Promise 也足夠了。

雖然上述內容在強制解析參數的情況下是“最並行的”(fsvo "parallel" 1取決於承諾來源),但並不總是需要它,並且在幾乎不需要的情況下會更慢。 如果調用不涉及 IO 或相關的延續,它可能可以避免async或涉及承諾。

1考慮這樣的concurrent而不是parallel可能更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM