在gulp任务中,我有以下代码创建一个gitAction承诺数组,这些承诺在Promise.all()
语句中执行。之后,我在then()
中进一步发表声明。但是,在git拉入all()已经终止之前,then()
被调用。有什么线索吗?
var git = require('gulp-git');
var gitActionPromise = function(repo, url) {
console.log('git action '+repo);
var pathToRepo = './repos/'+repo;
if (fs.lstatSync(pathToRepo).isDirectory()) {
return new Promise((resolve, reject) => {
git.pull('origin', 'master', {cwd: pathToRepo}, function (err) {
console.log(repo + " pull done!");
if (err) {
console.log('error');
reject(err);
} else {
console.log('ok');
resolve();
}
})
})
} else {
return new Promise((resolve, reject) => {
git.clone(url, {cwd: pathToRepo}, function (err) {
console.log(repo + " clone done!");
if (err) {
console.log('error');
reject(err);
} else {
console.log('ok');
resolve();
}
})
})
}
};
var repos = package.repos || {};
var promises = Object.keys(repos).map(function(repo) {
return gitActionPromise(repo, repos[repo]);
});
Promise.all(promises).then(
console.log('something else') <= this line was causing my issue
); needed to be enclosed in function
答案 0 :(得分:9)
您必须将功能传递给then
:
Promise.all(promises).then(function() {
console.log('something else');
});
您只需记录的代码&#34;其他内容&#34;马上。