javascript-流星与承诺

我一直试图养成使用Promise的习惯,但是在尝试在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还是有bug?

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

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")
    });



  });
}

解决方法:

(更新: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.注意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);
});
上一篇:Logback相关知识汇总


下一篇:javascript-TypeScript在Bluebird中使用ES5中的动态导入