我在 NodeJS
项目上工作,我在我的代码中使用了 Promise
要链接一些方法,我需要在“thens”链之一中中止
findEmployeeByCW('11111', "18-09-2016").
then(function () {
return findEmployeeByCWE('111111', "18-09-2016", '111111')
}, function () {
console.log('createEmployeeLoginBy')
createEmployeeLoginBy('111111', "18-09-2016", '111111').
then(function (log) {
SaveEmployeeLogToDb(log)
// ***************
// ^_^ I need to exit here ....
})
})
.then(function (log) {
return updateLoginTimeTo(log, '08-8668', '230993334')
}, function () {
return createNewEmployeeLog('224314', "18-09-2016",
'230993334', '08-99')
})
.then(SaveEmployeeLogToDb).then(DisplayLog).catch(function (e) {
console.log(e);
})
最佳答案
如果我理解正确的话,这里没有必要取消或抛出。
你应该能够通过重新安排来达到你的目的:
findEmployeeByCW('11111', "18-09-2016")
.then(function() {
return findEmployeeByCWE('111111', "18-09-2016", '111111')
.then(function(log) {
return updateLoginTimeTo(log, '08-8668', '230993334');
}, function(e) {
return createNewEmployeeLog('224314', "18-09-2016", '230993334', '08-99');
});
}, function(e) {
return createEmployeeLoginBy('111111', "18-09-2016", '111111');
})
.then(SaveEmployeeLogToDb)
.then(DisplayLog)
.catch(function(e) {
console.log(e);
});
前提是 log
对象始终通过到达该点的所有可能路径传送到 SaveEmployeeLogToDb
,如原始代码所暗示的那样。
关于javascript - 我怎么能中止 JavaScript Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39624712/