Новое обещание, пытаясь понять логический поток. Я думал, что понял, пока не начал вставлять ошибки в тест. В приведенном ниже примере, когда я прокомментирую 3-ю строку, почему Reject не возвращается в обещание?Q Обещание решить/отклонить логику - почему не обещание возвращается как отклонено?
var Q = require("q")
var getInstallBase = function() {
return new Promise(function(resolve, reject) { \t
\t //var IBdata = 'temp IBdata'; // <------comment out so IBdata not defined
if (IBdata){
resolve(IBdata); // State will be fulfilled
} else {
\t reject("error getting IBdata"); // State will be rejected \t \t \t
}
});
}
var mungeIt = function(IBdata){
return new Q.Promise(function(resolve,reject){
\t \t // get insight from data
\t \t console.log('IBdata = ' + IBdata);
\t \t 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) {
\t return new Promise(function(resolve, reject) {
\t \t console.log('insight = ' + insight);
\t \t // post result
\t \t var objectID = '12345';
\t \t if (objectID){
\t \t \t setTimeout(function() {
\t \t \t \t console.log('done waiting');
\t \t \t \t resolve(objectID);
\t \t \t }, 2000);
\t \t \t // State will be fulfilled
\t \t } else {
\t \t \t reject("error posting insight to object store"); // State will be rejected
\t \t }
\t });
};
(function extractInsightCycle() {
\t getInstallBase().then(mungeIt).then(postResults).then(function(objectID) {
\t \t console.log('object successfully posted, ID: ' + objectID)
\t \t extractInsightCycle();
\t }).catch(function(error) {
\t \t console.log('something went wrong', error);
\t \t extractInsightCycle();
\t })
})();
Что вы подразумеваете под словом «* Reject не возвращается в обещании»? Не выполняется ли обратный вызов 'catch'? – Bergi