例子一
function first(a, b) {
return a + b;
}
function second() {
return Math.floor(Math.sqrt(arguments[0]));
}
function third() {
return Math.PI * (arguments[0]);
}
Q.fcall(first, 3, 4)
.then(second)
.then(third)
.then(function (result) {
console.log(result);
});
示例 2
function first(a, b) {
var d = Q.defer();
if (isNaN(a) || isNaN(b)) {
d.reject('Wrong Numbers');
} else {
d.resolve(a + b);
}
return d.promise;
}
function second() {
return Math.floor(Math.sqrt(arguments[0]));
}
function third() {
return Math.PI * (arguments[0]);
}
first(3, 4)
.then(second)
.then(third)
.then(function (result) {
console.log(result);
}, function (error) {
console.log(error);
});
所以这两个示例之间有什么区别,因为据我自己的理解,示例 2 有错误和成功处理程序,而在示例 1 我们没有
最佳答案
这两个示例是类似的,但是,在第一个示例中,您正在调整一个基于非 promise 的函数,而第二个示例直接返回一个 promise。如果您正在编写自己的函数,您总是希望在其中返回 promise ,那么您的第二个示例将是首选。但是,如果要在基于非 promise 的上下文中使用这些功能,那么这当然是以适应性为代价的。 Q.fcall
更常用于使第三方功能适应基于 promise 的范例。
这是您还可以使用的第三个示例(这是我在编写基于 promise 的库时首选的做事方式):
function first(a, b) {
if (isNaN(a) || isNaN(b)) {
return Q.reject('Wrong Numbers');
} else {
return Q(a + b);
}
}
function second() {
return Q(Math.floor(Math.sqrt(arguments[0])));
}
function third() {
return Q(Math.PI * (arguments[0]));
}
first(3, 4)
.then(second)
.then(third)
.then(function (result) {
console.log(result);
})
.fail(function (error) {
console.log(error);
});
这通过使用以下内容删除了对 Q.defer()
的一些不必要的调用:
Q(value)
If value is a Q promise, returns the promise.
If value is a promise from another library it is coerced into a Q promise (where possible).
If value is not a promise, returns a promise that is fulfilled with value.
Q.reject(reason)
Returns a promise that is rejected with reason.
同样值得注意的是,Q 包含一整套方法来适应不同类型的函数、方法、nodejs 样式回调等,以适应基于 Q promise 的系统。自述文件中未提及其中许多内容,instead check the API reference .
关于javascript - Q.fcall 和 (Q.defer + Q.resolve + Q.promise)/Q 库的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37723363/