我正在使用' Q'用于在NodeJS中进行异步调用的库。
但是在其中一个用例中,我需要defer
promise
,直到完成所有异步调用。
public someFunction(files: string[]) : Q.Promise<string> {
var needSomeInfo;
var defer = Q.defer;
for (var i = 0; i < files.length; i++) {
_this.readFile(files[i]).then(function(res) {
needSomeInfo += res.Info;
j++;
if (j == files.length) {
defer.resolve(needSomeInfo);
}
}).fail(function(err) {
j++;
if (j == resultFiles.length) {
defer.resolve(needSomeInfo);
}
//this is kinda stupid. I need to wait till all file calls are done because of consolidated info I need from them
});
}
return defer.promise;
}