Я думаю, что ответ уже в вопросе в том, что .then()
, похоже, .pipe()
, который вы ищете.
Чего не хватает, так это того, что (result)
должно быть (results)
, т.е. массив всех пар {name, content}
, возникающих из Promise.mapSeries(urls, ...)
.
Promise.mapSeries(urls, url => {
return request.getAsync({'url':url, 'encoding':'binary'}).spread((response, body) => {
if (response.statusCode == 200) {
return {
'name': url.match(/\/([^/]*)$/)[1], // get the last part of url (file name)
'content': body
};
} else if (response.statusCode == 404) {
throw new Error(`The archive ${url.match(/\/([^/]*)$/)[1]} does not exist`);
} else {
throw new Error(`Unsuccessful attempt. Code: ${response.statusCode}`);
}
});
}).then((results) => {
// Here write each `result.content` to file.
}).catch((error) => {
console.error(error);
});
На практике вы, вероятно, не захотите писать так, потому что каждая getAsync()
должна быть завершена до начала какой-либо записи.
В большинстве случаев (и, вероятно, того, что вам нужно) лучше будет сделать так, чтобы содержимое каждого успешного getAsync()
было записано как можно скорее:
Promise.mapSeries(urls, url => {
let name = url.match(/\/([^/]*)$/)[1]; // get the last part of url (file name)
return request.getAsync({'url':url, 'encoding':'binary'}).spread((response, body) => {
if (response.statusCode == 200) {
// write `body.content` to file.
} else if (response.statusCode == 404) {
throw new Error(`The archive ${name} does not exist`);
} else {
throw new Error(`Unsuccessful attempt. Code: ${response.statusCode}`);
}
});
}).catch((error) => {
console.error(error);
});
Если пойти дальше, вы можете лучше обрабатывать ошибки, например, вы можете:
- перехватить отдельные URL / ошибки получения / записи
- компилировать статистику успеха / неудачи.
Нечто подобное может быть:
Promise.mapSeries(urls, url => {
let name = url.match(/\/([^/]*)$/)[1] || ''; // get the last part of url (file name)
if(!name) {
throw new RangeError(`Error in input data for ${url}`);
}
return request.getAsync({'url':url, 'encoding':'binary'}).spread((response, body) => {
if (response.statusCode == 200) {
// write `body.content` to file.
return { name, 'content': body };
} else if (response.statusCode == 404) {
throw new Error(`The archive ${name} does not exist`);
} else {
throw new Error(`Unsuccessful attempt. Code: ${response.statusCode}`);
}
})
.catch(error => ({ name, error }));
}).then((results) => {
let successes = results.filter(res => !res.error).length;
let failures = results.filter(res => !!res.error).length;
let total = results.length;
console.log({ successes, failures, total }); // log success/failure stats
}).catch((error) => {
console.error(error); // just in case some otherwise uncaught error slips through
});