Pregunta

I'm trying to use q.all to manage the process of multiple npm install module_name methods.

Strangely, it appears as though the promise is being returned before the promise is completed.

installNodeModules: function(module_list){
            function installModule(module_name){
                var defer = q.defer();
                console.log(module_name);
                exec_child_process('npm install '+module_name, function(err,stdout,stderr){
                    if(err) defer.reject(err);
                    defer.resolve();
                    return defer.promise;
                });
            }

            var promise_array = [];
            for (var i =0; i<module_list.length;i++){
                promise_array.push(installModule(module_list[i]));   
                console.log('promises array created');
            }

             q.all(promise_array).done(function(){
                    console.log('promises returned');   
                });
        },

I've tried using both the done & then methods after the all, but same result, the promise is returned before npm install has completed.

¿Fue útil?

Solución

installModule should be written like this:

        function installModule(module_name){
            var defer = q.defer();
            console.log(module_name);
            exec_child_process('npm install '+module_name, function(err, stdout, stderr){
                if(err) { 
                   defer.reject(err);
                } else {
                   defer.resolve();
                }
            });
           return defer.promise;
        }

It is installModule which should return the promise, not the callback.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top