如何在nodeJS中使用async.eachSeries



我想以同步的方式迭代项目列表,并在下一步中使用每个步骤的结果。我能有人纠正/建议我正在使用的代码逻辑吗?

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')
}
)

相关内容

  • 没有找到相关文章

最新更新