承诺的节点模式

时间:2014-03-13 21:10:28

标签: javascript node.js design-patterns asynchronous promise

我遇到了节点问题。我想调用一个数据访问对象,可能还要调用其中的其他对象,一旦完成呈现Jade模板

类似的东西:

 provider1.getData(args, function(error, results) {

   /* do something with each result element */
   for(int i = 0l i < results.length; i++) {
     provider2.getData(args, function(error, items) {

        store.push(items);
     });
   }
 });
 /* Here I want to ensure that the above operations are complete */
 result.render( .... , {
   data:store
 });

基本上,我想确保在使用数据呈现模板之前完成数据检索。目前,渲染发生时不会填充变量存储。我看过promises看起来很有希望。有没有人有一个简洁的解决方案将我的代码示例转换为同步结构?

2 个答案:

答案 0 :(得分:3)

这是一个承诺答案(假设Bluebird)。我觉得它更清洁了:

// convert to promise interface, it's possible to do this on a single method basis
// or an API basis, it depends on your case - it's also _very_ fast. 
Promise.promisifyAll(Object.getPrototypeOf(provider1.prototype)); 
Promise.promisifyAll(Object.getPrototypeOf(provider2.prototype));

//note the async suffix is added by promisification.
provider1.getDataAsync(args).then(function(results) {
   return Promise.map(results,provider2.getDataAsync.bind(provider2));
}).then(function(results){
    //results array here, everything is done and ready,
});

与承诺一样,如果您有错误,只需throw

答案 1 :(得分:0)

您应该尝试使用async库。

provider1.getData(args, function(error, results) {

   /* do something with each result element */
   async.each(results,
                       function(result, cb) { // called for each item in results
                           provider2.getData(args, function(error, items) {

                                  store.push(items);
                                  cb(error);
                            });
                       },
                       // final callback
                       function (err) {
                           if (!err) {
                             /* Here I want to ensure that the above operations are complete */
                              result.render( .... , {
                                     data:store
                               });
                           }
                       }
                    );

}