This code will help you when you have a list of asynchronous tasks to perform, for example a list of files that you have to upload to the server from nodejs.
function prueba() {
return new Promise((resolve, reject) => {
console.log("Promise start");
resolve();
})
}
var files = obj_ips.files;
var promises = [];
files.forEach(function(elem) {
promises.push(
prueba().then((res) => {
console.log("Promise ended");
}).catch((err) => {
})
);
});
Promise.all(promises).then(() => {
console.log("All promises are completed.");
})
Promise start Promise start Promise ended Promise ended All promises are completed.
Rafael Jose Garcia Suarez [email protected]