Q.fcall 和 (Q.defer + Q.resolve + Q.promise) /Q 库的区别

Difference between Q.fcall and (Q.defer + Q.resolve + Q.promise) /Q Library

示例 1

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 个示例之间有什么区别,因为根据我自己的理解,在 示例 2 中,我们有错误和成功处理程序,并且在 示例 1 我们没有

这两个示例是类似的,但是,在第一个示例中,您正在调整一个基于非承诺的函数,而第二个示例直接 return 是一个承诺。如果您正在编写自己的函数,您总是希望 return 承诺您的第二个示例将是首选。但是,如果要在基于非承诺的上下文中使用这些功能,那么这当然是以适应性为代价的。 Q.fcall 更常用于使第三方功能适应基于承诺的范例。

这是您也可以使用的第三个示例(这是我在编写基于 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 的系统。其中很多在readme中都没有提到,instead check the API reference.