流星与承诺

时间:2015-05-20 19:00:59

标签: javascript meteor promise bluebird

我一直在努力养成使用promises的习惯,但在尝试在Meteor环境中的服务器端代码中使用它们时会遇到问题。这就是问题所在:

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
    p = function(){
      return new Promise(function(res,rej) {
        res("asd");
      });
    };
    p().then(function(asd){
      console.log("asd is " + asd);
      return "zxc"
    }).then(Meteor.bindEnvironment(function(zxc){
      console.log("zxc is " + zxc);
      return "qwe"
    })).then(function(qwe){
      console.log("qwe is " + qwe);
    });
  });
}
已安装

mvrx:bluebird

代码也可在GitHub

获取

预期产出:

asd is asd         
zxc is zxc
qwe is qwe

实际输出:

asd is asd         
zxc is zxc
qwe is undefined

删除Meteor.bindEnvironment包装修复了问题,但我需要它才能在回调中使用Collections

那我在这里缺少什么?是不是可以这样使用Promises + Meteor还是有错误?

我实际上要完成的是具有重要部分结果但需要同步结束的并行管道。这样的事情。

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup


    promises = [];

     step1 = function(input){
        return new Promise(function(res, rej){
          console.log(input + ":Step 1");
          res(input);
        });
      };
      step2 = function(input){
        return new Promise(function(res, rej){
          console.log(input + ":Step 2");
          res(input);
        });
      };
      step3 = function(input){
        return new Promise(function(res, rej){
          console.log(input + ":Step 3");
          res(input);
        });
      };
      slowIO = function(input){
        var inp = input;
        return new Promise( function(res,rej){
          setTimeout(function(){
            console.log(inp + ":SlowIO");
            res(inp);
          },Math.random()*20000);
        });
      };
      end = function(input){
        return new Promise(function(res,rej){
          console.log(input + ": done, commiting to database");
          res()
        });
      };

    for (var i = 0; i < 100; ++i) {
      promises.push(step1("pipeline-" + i).then(step2).then(slowIO).then(step3).then(end));
    };

    Promise.all(promises).then(function(){
      console.log("All complete")
    });



  });
}

2 个答案:

答案 0 :(得分:3)

(更新:I've logged an issue on github to see if it can be resolved.

使用此方式时,Meteor.bindEnvironment似乎存在问题。

If it's called from outside a Fiber it won't return it's value. 注意return

之前丢失的Fiber(runWithEnvironment).run()

目前一个简单的解决方案是返回Promise而不是结果:

// when passed as a callback to `Promise#then`
//  allows it to resolve asynchronously
var asyncThen = function(fn){
  return function(arg){
    return new Promise(function(resolve, reject){
      fn(arg, resolve, reject);
    })
  };
};

Promise.resolve("asd").then(function(asd){
  console.log("asd is " + asd);
  return "zxc"
}).then(
  asyncThen(
    Meteor.bindEnvironment(function(zxc, resolve, reject){
      console.log("zxc is", zxc);
      resolve("qwe");
    })
  )
).then(function(qwe){
  console.log("qwe is " + qwe);
});

答案 1 :(得分:1)

承诺只是一种以同步方式编写异步代码的方法。 如果那是你的全部,为什么不使用Meteor.wrapAsync()?在你的情况下,你有zxc骑牛仔自己的纤维&amp;谁知道什么时候回来。蓝鸟是伟大的&amp;在客户端上超级快,但我认为使用Meteor为您提供的代码更清晰:

//*UNTESTED*//
asd = function() { return 'foo';};
asdSync = Meteor.wrapAsync(asd);
asdResult = asdSync();

qwe = function(input) {return input.reverse()};
qweSync = Meteor.wrapAsync(qwe);
qweResult = qweSync(asdResult); //should return 'oof'
相关问题