与蓝鸟的承诺链阵列 [英] chain array of promises with bluebird

查看:142
本文介绍了与蓝鸟的承诺链阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个工作我的诺言方式我坚持我的用例。
我有变压器的功能(每个功能是一种承诺,并修改一些JSON结构)。

I'm a working my way with promises and I'm stuck with my use case. I have an array of transformer functions (each function is a promise and modifies some JSON structure).

让我告诉一些code。

Let me show some code.

让我们说这是我的JSON结构(阵列)

Lets say this is my JSON structure (array)

var data = [{a: 1, b:2}, {a:3, b:4}]; 

transformFunction 是变换函数修改数据以某种方式的定义。这两个函数添加 C D 属性上面的JSON结构:

transformFunction is definition of transform functions modifying the data a certain way. The two functions adds c and d property to the above JSON structure:

var transformFunctions = { // 

    transform1: function (data) {  // This function adds `c` property to each object from `a`
        return new Promise(function (resolve) {
             for (var i = 0; i < data.length; i++) {
                 data[i].c = data[i].a;
             }
             return resolve(data);
        })
    },

    transform2: function (data) {  // This function adds `d` property to each object from `c`
        return new Promise(function (resolve) {
             for (var i = 0; i < data.length; i++) {
                 data[i].d = data[i].c;
             }
             return resolve(data);
        })
    },
    ...
}

该从UI用户指定他应该以什么顺序使用的变压器的功能和。比方说,他选择了正常秩序是这样的:

The from a UI user specifies which transformer functions he should use and in what order. Lets say he picked the normal order like this:

var userTransformList = ['transform1', 'transform2'];

transform1 方法应该修改数据,并将结果传递给 transform2 方法。

The transform1 method should modify the data and the result should be passed to transform2 method.

我一直在寻找: Promise.all ,但它似乎并不关心承诺的顺序,以及最重要的,它需要通过previous结果到下一个承诺。

I was looking at: Promise.all but it seems that it does not care for the order of the promises, and most important it needs to pass the previous result to the next promise.

推荐答案

注意:作为adeneo在评论中指出,使用承诺只有当你正在处理异步code

Note: As adeneo pointed out in the comments, use promises only if you are dealing with asynchronous code.


  1. 创建的这是要被执行的功能的阵列。并确保他们都返回的承诺。

  1. Create an array of functions which are to be executed. And make sure that they all return a Promise.

然后,您可以使用<一个href=\"https://github.com/petkaantonov/bluebird/blob/master/API.md#reducefunction-reducer--dynamic-initialvalue---promise\"相对=nofollow> Promise.reduce 通过返回执行当前的承诺返回函数的结果,在每次迭代减少初始值变换终值

Then, you can use Promise.reduce to reduce the initial value to the transformed final value by returning the result of executing current promise returning function, on every iteration.

最后,你可以附加一个然后处理程序,以获取实际值和的处理程序,只在情况下,如果承诺被拒绝。

Finally you can attach a then handler to get the actual value and a catch handler, just in case if the promises are rejected.

假设我们有两个变换函数是这样的。

Lets say we have two transform functions like this.

注意:我再告诉。你永远不应该使用承诺像这些功能。您应该使用的承诺,只有当你正在处理的功能真的是异步的。

Note: I am telling again. You should never use Promises with functions like these. You should use promises only when the functions you are dealing with are really asynchronous.

// Just add a property called `c` to all the objects and return a Promise object
function transform1(data) {
    return Promise.resolve(data.map(function(currentObject) {
        currentObject.c = currentObject.a + currentObject.b;
        return currentObject;
    }));
}

// Just add a property called `d` to all the objects and return a Promise object
function transform2(data) {
    return Promise.resolve(data.map(function(currentObject) {
        currentObject.d = currentObject.a + currentObject.b + currentObject.c;
        return currentObject;
    }));
}

然后就可以改造原有的价值,像这样

Then you can transform the original value, like this

Promise.reduce([transform1, transform2], function (result, currentFunction) {
        return currentFunction(result);
    }, [{a: 1, b: 2}, {a: 3, b: 4}])      // Initial value
    .then(function (transformedData) {
        console.log(transformedData);
    })
    .catch(function (err) {
        console.error(err);
    });

输出

[ { a: 1, b: 2, c: 3, d: 6 }, { a: 3, b: 4, c: 7, d: 14 } ]

这篇关于与蓝鸟的承诺链阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆