2

我的问题是当您需要将上下文和参数传递给构建承诺的函数时, BlueBird中的承诺的优雅并行化。

为了使我的问题易于理解和测试,我做了一个不依赖的例子。

假设我进行涉及异步“计算机”(必须释放其资源)的计算( 1/(x x x) + 1/(x*x) )。正方形和立方体是异步独立计算的。

我可以这样计算:

InitComputer(2) // returns a promise
.then(invert)
.then(function(arg){
    return Promise.all([
        proto.square(arg),
        proto.cube(arg)
    ]);
}).spread(function(sq, cu){
    this.set(sq + cu);
}).catch(function(err){
    console.log('err:', err);
}).finally(endComputer);

但与理论上可能的情况相比,我发现这种用法all太重了。当您将函数作为参数传递给 时then,它会被执行。当您将函数传递给 时all,它们不是,存在错误。我怀疑我缺少实用程序或模式...

有没有一种解决方案可以将其更改为这种风格的更简单的东西:

InitComputer(2)
.then(invert)
.all([
    proto.square,
    proto.cube
]).spread(function(sq, cu){
    this.set(sq + cu);
}).catch(function(err){
    console.log('err:', err);
}).finally(endComputer);

?

我可能会破解Promise.prototype.all或定义一个新函数以避免增加多态性,但我只对不涉及修改我不拥有的对象的解决方案感兴趣。


附件:

以下是我的测试如何定义“计算机”:

var Promise = require("bluebird");

function Computer(){}
function InitComputer(v){
    // initializing a computer is asynchronous and may fail hence the promise
    var c = new Computer(), resolver = Promise.defer();
    setTimeout(function(){
        if (v>1) resolver.resolve(v);
        else resolver.reject(new Error("bad value: "+v));
    },100);
    return resolver.promise.bind(c);
}
var proto = Computer.prototype;
proto.square = function(x){
    // imagine this really uses the computer and is asynchronous
    if (!this instanceof Computer) throw new Error('not a computer');
    return x*x
}
proto.cube = function(x){ return x*x*x }
proto.set = function(v){ this.value = v }

function endComputer(){
    // releases resources here
    console.log('value:', this.value);
}

// this asynchronous function doesn't involve or know the computer
function invert(v){ return 1/v }
4

2 回答 2

3

你不必在Promise.all那里使用。而不是这样做:

.then(function(arg){
    return Promise.all([
        proto.square(arg),
        proto.cube(arg)
    ]);
}).spread(...

您可以简单地使用:

.then(function(arg){
    return [proto.square(arg), proto.cube(arg)];
}).spread(...

如果我们在 node.js 中有箭头函数,它会很简单:

.then(arg => [proto.square(arg), proto.cube(arg)]).spread(...

Promise.all当你需要用至少 2 个 Promise启动一系列 Promise 时使用。例如:

var promise1 = somePromise();
var promise2 = somePromise2();

// Start the chain here
Promise.all([promise1, promise2])
.spread(function(value1, value2) {
    // ...
});
于 2014-01-09T08:59:14.150 回答
1

对于您提到的资源用例管理,bluebird 有Promise.using(). Promise.using()让您设置disposer()功能以在完成使用后自动关闭异步检索的资源

Promise.join()也将有助于组合 thecubesquareasync 方法的结果

在这里,我稍微重写了您的InitComputer示例以说明其工作原理-它现在返回Computer实例,其中将 val 添加为属性,而不是 val,并且我也将其放置endComputer在原型上

注意:您始终可以Promise.method()像这样使用而不是返回延迟:

var invert = Promise.method(function invert(v){ return 1/v })

新的初始化计算机:

function InitComputer(v){
    var c = new Computer(), resolver = Promise.defer();
    setTimeout(function(){
        if (v>1) {
            c.val = v;
            resolver.resolve(c);
        }
        else resolver.reject(new Error("bad value: "+v));
    },100); /** notice resource disposer function added below **/
    return resolver.promise.bind(c).disposer(function(compu){compu.endComputer()});
}

新代码:

Promise.using(InitComputer(1.2), function(computer){
    return invert(computer.val)
    .then(function(inverted){
        return Promise.join(computer.square(inverted), computer.cube(inverted), 
            function(sq, cu){
                computer.set(sq + cu)
            }
        )
    })
    .catch(function(err){
        console.log('err:', err);
    });
})
于 2014-07-07T22:34:18.057 回答