(节点)运行n次的动态承诺

时间:2015-09-01 16:50:08

标签: javascript node.js dynamic promise

我有一个包含用户信息的对象数组。

var names=[{name: 'yoda', 
            address:'123 Compton', 
            email:'yodalicious@force.com'},
            {name: 'darth vader', 
            address:'69 harlem', 
            email:'elevader@force.com'},{....}]

这是一个范围从100到1000个用户的列表。 问题是我需要从Web和DB请求/提取有关每个用户的信息,并创建一个包含有关每个用户的更多详细信息的新数组。这将是3个来源。现在每次拉动需要10毫秒到1分钟。 这就是我跳到承诺的地方。 (“native-promise-only”),例如:

require("native-promise-only");

function getFile(file) {
    return new Promise(function(resolve){
        fakeAjax(file,resolve);
    });
}
//Recursive call
function recursivePromise(originalArray, newArray){
 if(isEmpty(originalArray) ){
  store(newArray);
  display(newArray);
}
else{
  var currentItem = originalArray[0];
  var p1 = getFile(currentItem.name);
  var p2 = getFile(currentItem.address );
  var p3 = getFile(currentItem.email);

  var newDataItem={};
  p1.then(function(msg){
    //TODO check status
    newDataItem.nameinfo= msg;
    return p2;
  })

  .then(function(msg){
    //TODO check status
    newDataItem.addressinfo= msg
    return p3;
  })

  .then(function(msg){
    newDataItem.emailinfo= msg
    newArray.add(newDataItem);
    recursivePromise(originalArray.shift(), newArray)
  });
}
}
var new_array=[];
recursivePromise(names, new_array);

这是一个粗略的代码,我有类似的东西,它的工作原理!有些。但是我的一个错误告诉我,我可能会设置未来的失败。我是以递归方式进行的,因为“名称”中项目的位置很重要。所以他们需要按顺序处理。

1 个答案:

答案 0 :(得分:0)

Issues as I see them :

Coding errors :

  • fruits comes out of nowhere; presumably originalArray.shift() was intended.
  • array.shift() removes the 0th element from the array and returns the removed element, not the array.
  • originalArray would be destroyed, which is not a good idea.
  • .add() is not an array method, presumably .push() was intended.

Pattern issues :

  • The p1.then(...).then(...) chain is a rather cumbersome way to build newDataItem; Promise.all(p1, p2, p3).then(...) would be more readable, with newDataItem built in a single expression.
  • Recursion is not necessary; the process would be more simply coded as ...; Promise.all(arrayOfPromises).then(function(newArray) {...}); (that's a second use of Promise.all()); although the promises will settle in any order they like, newArray will be congruent with originalArray.
  • newArray can be generated internally, without needing to pass in [].
function process(originalArray) {
    var promises = originalArray.map(function(currentItem) {
        var p1 = getFile(currentItem.name);
        var p2 = getFile(currentItem.address);
        var p3 = getFile(currentItem.email);
        return Promise.all([p1, p2, p3]).then(function(results) {
            return {
                nameinfo: results[0],
                addressinfo: results[1],
                emailinfo: results[2]
            };
        });
    });
    return Promise.all(promises).then(function(newArray) {
        display(newArray);
        return newArray;
    });
}
process(names).then(function(newArray) {
    store(newArray);
});

display(newArray) and store(newArray) could both be executed internally or externally. I split them above to demonstrate the two possibilities.