I'm struggling a bit for a preferred way to organize a sequence of asynchronous tasks that can be applied in parallel. Say, you are parsing data from many files. In my case I'm using javascript and promises, but this could be most any language. (Hence the weird tags as "javascript" and "language-agnostic".
Option A: Parallelize at the end
1) First, create the chain of tasks for a single file / stream, e.g.
function readAndParseAndConvert(file) { return read(filename) .then((body) => parse(body)) .then((parsed) => convert(parsed)); }
2) Then, put it all together
Promise.all(theArrayOfFilenames.map(readAndParseAndConvert));
Option B: Parallelize each step
1) Create the steps
function readFiles(filenames) { return Promise.all(filenames.map((filename) => read(filename)) } function parseBodies(bodies) { return Promise.all(bodies.map(body) => parse(body)) } function convertAll(parsed) { return Promise.all((parsed) => convert(parsed)); }
2) Put them together
readFiles(filenames) .then(parseBodies) .then(convertAll);
Ultimately this may get flagged as "opinion based", but any objective thoughts? Remember that real code would have try/catch, closing files, etc...