I've a function, which returns a promise, inside that I call another function, and the status of this promise is based on the status of the inner promise.
Is there a way to shorthand this process. Look at the example below.
function foo(bar) {
var deferred = Q.defer();
switch (bar) {
case 'baz1':
deferred.resolve();
break;
case 'baz2':
deferred.reject();
break;
case 'this_is_how_i_do_it':
funReturningPromise().then(function (value) {
deferred.resolve(value);
}, function (err) {
deferred.reject(err);
});
break;
case 'can_we_do_it_like_this':
// can we do something like this, which will resolve or reject the deferred,
// based on the status promise returned by funReturningPromise().
// 'chain' is just a name
funReturningPromise().chain(deferred);
break;
}
return deferred;
}
Thanks,
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…