Promise.method
Promise.method(function(...arguments) fn) -> function
返回包装了给定函数 fn
的新函数。新函数将始终返回一个 promise,这个 promise 使用原始函数返回值履行,或者使用来自原始函数抛出的异常进行拒绝。
当一个函数有时可以同步返回或者同步抛出时,这个方法很方便。
不使用 Promise.method
的例子:
MyClass.prototype.method = function(input) {
if (!this.isValid(input)) {
return Promise.reject(new TypeError("input is not valid"));
}
if (this.cache(input)) {
return Promise.resolve(this.someCachedValue);
}
return db.queryAsync(input).bind(this).then(function(value) {
this.someCachedValue = value;
return value;
});
};
使用 Promise.method
,不需要在 promise 中手动包装直接返回值或抛出值:
MyClass.prototype.method = Promise.method(function(input) {
if (!this.isValid(input)) {
throw new TypeError("input is not valid");
}
if (this.cache(input)) {
return this.someCachedValue;
}
return db.queryAsync(input).bind(this).then(function(value) {
this.someCachedValue = value;
return value;
});
});