为什么我的最后一个.then(writeLin..)没有运行?
注意:triggercommand
返回返回promise 的函数
.then(function () {
if (fs.existsSync(tempDir + '/' + repoName)) {
return self.triggerCommand("git", ["checkout", "master"], {cwd: tempDir + '/' + repoName})()
.then(
self.triggerCommand("git", ["pull", "master"], {cwd: tempDir + '/' + repoName})
)
}
return self.triggerCommand("git", ["clone", remote], {cwd: tempDir});
}
)
.then(
writeLine("Git clone/pull complete.")//this never runs
)
.finally(function () {
//this runs
根据Promises规范,then
函数应该接收一个函数,该函数将在promise被解析时被调用。
writeLine("Git clone/pull complete.")
将在配置then
链的那一刻运行,因为它未封装在函数中。
修复方法很简单:
.then(function () {
if (fs.existsSync(tempDir + '/' + repoName)) {
return self.triggerCommand("git", ["checkout", "master"], {cwd: tempDir + '/' + repoName})()
.then(
self.triggerCommand("git", ["pull", "master"], {cwd: tempDir + '/' + repoName})
)
}
return self.triggerCommand("git", ["clone", remote], {cwd: tempDir});
}
)
.then(function(){
writeLine("Git clone/pull complete.")
})
.finally(function () {
//this runs
})