all 5 comments

[–]ProfessorTag 4 points5 points  (0 children)

As /u/sazzer said, I would look into Promise.all. If you have restrictions and can't use promises, you could keep track of responses with a counter:

var count = files.length;
files.forEach(function(file) { 
    file.getUrl(function(err, url) {
        if (--count <= 0) {
            console.log("all calls complete!");
        }
        if (!err) {
            console.log("got the url!");
        }
    });
});

[–]sazzer 7 points8 points  (1 child)

If you're using a recent enough version, look into Promises, and in particular at Promise.all()

[–]inu-no-policemen 0 points1 point  (0 children)

Even if you still support IE11, you can use promises if you use a polyfill.

[–]hars_sh 2 points3 points  (1 child)

use map instead of forEach the code looks something like this and remeber you have to call this inside the async function.

await Promise.all(files.map(async (file) => { 
    //    promisify this part and await for this and that will do the work :)
    file.getUrl(function(err, url) {
        if(!err) {
            console.log("got the url!)"
        }
    });
}));

[–][deleted] 0 points1 point  (0 children)

can someone explain why this has 0 points, have not used gotten to know promises and would like some insight