[英]q.all being returned before promise is completed
I'm trying to use q.all
to manage the process of multiple npm install module_name
methods. 我正在尝试使用q.all
来管理多个npm install module_name
方法的过程。
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. 我尝试过在all
之后使用done
和then
方法,但是同样的结果,在npm install
完成之前返回了promise。
installModule
should be written like this: installModule
应该这样写:
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. 是installModule
,它应该返回promise,而不是回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.