【发布时间】:2016-06-10 12:12:09
【问题描述】:
promise 新手,试图理解逻辑流程。以为我明白了,直到我开始插入错误进行测试。在下面的示例中,当我注释掉第 3 行时,为什么 Promise 中没有返回 Reject?
var Q = require( "q" )
var getInstallBase = function() {
return new Promise(function(resolve, reject) {
//var IBdata = 'temp IBdata'; // <------comment out so IBdata not defined
if (IBdata){
resolve(IBdata); // State will be fulfilled
} else {
reject("error getting IBdata"); // State will be rejected
}
});
}
var mungeIt = function(IBdata){
return new Q.Promise(function(resolve,reject){
// get insight from data
console.log('IBdata = ' + IBdata);
var insight = 'temp insight';
if (insight){
resolve(insight); // State will be fulfilled
} else {
reject("error getting insight"); // State will be rejected
}
})
}
var postResults = function(insight) {
return new Promise(function(resolve, reject) {
console.log('insight = ' + insight);
// post result
var objectID = '12345';
if (objectID){
setTimeout(function() {
console.log('done waiting');
resolve(objectID);
}, 2000);
// State will be fulfilled
} else {
reject("error posting insight to object store"); // State will be rejected
}
});
};
(function extractInsightCycle() {
getInstallBase().then(mungeIt).then(postResults).then(function(objectID) {
console.log('object successfully posted, ID: ' + objectID)
extractInsightCycle();
}).catch(function(error) {
console.log('something went wrong', error);
extractInsightCycle();
})
} )();
【问题讨论】:
-
“Reject 不会在 Promise 中返回”是什么意思?
catch回调不会执行吗?
标签: javascript node.js promise q