如何在nodeJS中使用async.eachSeries?

How to use async.eachSeries in nodeJS?

我想以同步方式遍历项目列表,并在下一步中使用每一步的结果。有人可以 correct/suggest 我正在使用的代码逻辑吗?

const async = require('async')

async.eachSeries([1, 2, 3, 4, 5],
  function downloadChunk (chunkID, asyncCallback) {
    console.log(chunkID)
    const result = `This is a result from ${chunkID} call and should be used somewhere in ${chunkID + 1}`
    // How should I pass this result to next step
    asyncCallback()
  },
  function complete (err) {
    if (err) console.log('Error: ' + err)
    console.log('this is the end. All the variables have been used')
  }
)

您可以像这样简单地在 async.eachSeries 之外创建一个变量:

const async = require('async')
var result = null;

async.eachSeries([1, 2, 3, 4, 5],
    function downloadChunk (chunkID, asyncCallback) {
        console.log(chunkID)
        console.log('Result from previous call', result);

        // reassign new value to the result
        result = `This is a result from ${chunkID} call and should be used somewhere in ${chunkID + 1}`

        asyncCallback()
    },
    function complete (err) {
        if (err) console.log('Error: ' + err)
        console.log('this is the end. All the variables have been used')
    }
)