Native Promises
Take a look at the native Promise object's static methods resolve
and reject
.
function foo(options) {
if(!isValid(options)) {
return Promise.resolve();
}
return promisifiedThirdPartyApi(options);
}
Angular $q
Use $q.when
to return a resolved Promise from some non-Promise object:
function foo(options) {
if(!isValid(options)) {
return $q.when([]);
}
return promisifiedThirdPartyApi(options);
}
Q Promises
Use Q.resolve()
which returns a resolved promise.
function foo(options) {
if(!isValid(options)) {
return Q.resolve();
}
return promisifiedThirdPartyApi(options);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…