Skip to content

Instantly share code, notes, and snippets.

@rawars
Last active June 20, 2020 19:23
Show Gist options
  • Save rawars/7fa951391d90f28c9d1ee37b03d6109a to your computer and use it in GitHub Desktop.
Save rawars/7fa951391d90f28c9d1ee37b03d6109a to your computer and use it in GitHub Desktop.
This code will help you when you have a list of asynchronous tasks to perform

Tail of promises

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.");
})

PROMPT:

Promise start Promise start Promise ended Promise ended All promises are completed.

Contact:

Rafael Jose Garcia Suarez [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment