Quiero iterar a través de una lista de elementos de manera sincrónica y usar el resultado de cada paso en el siguiente paso. ¿Alguien puede corregir/sugerir la lógica del código que estoy usando?
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') } )Simplemente puede crear una variable fuera de async.eachSeries así:
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') } )