为什么我的上一个.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
答案 0 :(得分:2)
根据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
})