I am migrating an existing program to use async
/await
(via Babel
's bluebirdCoroutines
) in order to learn this style. I have been looking at this tutorial.
I am a bit troubled by the following behavior. This snippet works as expected:
let parts = [];
let urlsP = urls.map((url, index) => {
return dlPart(url, index, tempDir);
});
for (let urlP of urlsP) { // Parallel (yay!)
parts.push(await urlP);
}
for (let part of parts) { // Sequential
await appendFile(leFile, part);
}
Re-written as follows, it still works but the fist operation is not parallel any more (it takes much longer to finish)!
let index = 0;
let parts = [];
for (let url of urls) { // NOT Parallel any more!!!
index++;
parts.push(await dlPart(url, index, tempDir));
}
for (let part of parts) {
await appendFile(leFile, part);
}
This is the implementation of dlPart()
function dlPart(url, num, dir) {
var cmd = 'wget --quiet "' + url + '" -O ' + dir + "/" + num;
return exec(cmd).then(() => {
return dir + '/' + num;
});
}
What am I missing?
The .map
function is asynchronous so the rest of your code doesn't have to wait on it, it will finish when ready. Then you replaced it with a for loop
that holds everything back while it completes.