1

In the method "myMethod" of my "gulpfile.js" I want to create multiple Promises. The quantity depends on the array size (method parameter). When I call the method I want to make sure that all promises are fulfilled before I continue. I would prefer to return not before all promises are fulfilled.

Please have a look at the last five lines of code.

Dependencies

var promiseAll = require('gulp-all');
var del = require('del');
var deleteEmpty = require('delete-empty');

gulp-all | del | delete-empty

Helper Method

var oneForAllPromises = function(promises){
    var promAll = promiseAll(promises);
    promAll.then(function(param) {
        console.log('foo');
    }, function(err) {
        console.error('foo');
    });
    return promAll;
}

Problematic Code

var myMethod = function(array1, array2){
    var promise = del(array1, {force: true});
    promise.then(paths => {console.log('foo');});

    var promises = [];
    promise.then(()=>{
        for(var i=0; i<array2.length; i++){
            promises[i] = new Promise(function(resolve, reject) {
                deleteEmpty(array2[i], {force: true},
                    function(err, deleted){
                        if(err){
                            console.log('foo');
                            reject
                        }else{
                            console.log('foo');
                            resolve
                        }
                    }
                );
            });
        }
    });

    // PROBLEM: Returns empty promises array
    console.log("promiesesLENGTH: "+promises.length); // promiesesLENGTH: 0

    // Create one promise for all the promises
    return oneForAllPromises(promises);
}
Ironori
  • 570
  • 2
  • 6
  • 19

1 Answers1

4

At the time of the console.log, the first promise promise = del(array1, {force: true}); is not yet finished, so none of the code in the then is yet executed. That's why your promises are empty.

You can simply return in a then another promise:

var myMethod = function(array1, array2){
    var promise = del(array1, {force: true});

    return promise.then(() => {
        return Promise.all(array2.map(array2value => {
            return new Promise(function(resolve, reject) {
                deleteEmpty(array2value, {force: true}, (err, deleted) => {
                    if (err) {
                        reject(err);
                    } else{
                        resolve()
                    }
                });
            });
        }
    });
}
Chris'
  • 13,624
  • 1
  • 15
  • 14
  • What triggers a promise to start then? – Ironori May 06 '16 at 22:02
  • 2
    The function passed to a promise's `then` is called when the promise's asynchronous operation completes, and will _never_ be called synchronously. You seem to be confused about how asynchrony works. Here's a good guide about the topic: https://github.com/getify/You-Dont-Know-JS/tree/master/async%20%26%20performance – JLRishe May 06 '16 at 22:10
  • The word "then" is meant to be part of the language here not a reference to the code. Sorry for the confusion. What the keyword "then" means in the code should be clear. But why do you write "the first promise ... is not yet started"? I thought it is started at the position where it is written but is most certainly not finished when executing the console log with the array length. Thank you for the guide. – Ironori May 07 '16 at 07:45
  • Yes indeed it is started but not finished. Sorry for misleading you – Chris' May 07 '16 at 16:22