javascript - 有希望的 meteor

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



  });
}

最佳答案

(更新: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()

之前缺少的 return

目前一个简单的解决方案是返回一个 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);
});

关于javascript - 有希望的 meteor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30358232/

相关文章:

javascript - 为什么 Meteor 会这样渲染 grid html?

meteor - 在 Meteor 中,如何找到哪个 session 变量触发了模板重新运行

http - .then 处理程序被乱序调用

javascript - 在解决之前,我的 jquery $.Deferred 不会阻止另一个 $.ajax 调用的发生

javascript - Node.js 异步控制流,有 3 个进程处理共享数据

javascript - 日期或文本类型的 HTML 输入在 SVGforeignObject 中不起作用

javascript - 用 Parsley 验证多个字段?

确保字符串每 n 个字符有空格的 Javascript 函数

javascript - 文本区域自动滚动到底部

node.js - Google Compute Engine、Nginx 和 Meteor 上的 502 错误网关