使用Meteor.wrapAsync()进行菊花链式NPM包功能

时间:2015-12-22 16:06:50

标签: javascript node.js asynchronous meteor npm

我正在尝试在我的meteor应用程序中使用Node包。在节点中,我需要将调用菊花链接在一起,如下所示:

return client.auth(req.session.token, req.session.sec).user("_SELF_").find(function(err, body, headers) {
    if (err) {
      console.log(err);
    }
    if (body) {
      console.dir(body);
    }
    if (body) {
      return res.send(body.results[0]);
    }
  });

我尝试过几件事但没有成功。如:

  var authSync = Meteor.wrapAsync(client.auth, client);
  return authSync(req.session.token, req.session.sec).user("_SELF_").find(function(err, body, headers) {
        if (err) {
          console.log(err);
        }
        if (body) {
          console.dir(body);
        }
        if (body) {
          return res.send(body.results[0]);
        }
      });

1 个答案:

答案 0 :(得分:0)

如果我错了,请纠正我,但似乎只有find()方法是异步的。

因此,client.auth(req.session.token, req.session.sec).user("_SELF_")将使用find()方法返回一些对象,这是您真正需要包装的内容。

var user = client.auth(req.session.token, req.session.sec).user("_SELF_");
var findAsync = Meteor.wrapAsync(user.find, user);
try {
  var body = findAsync();
  return res.send(body.results[0]);
} catch(e) {
  console.log(e);
}

它将返回result.send(...)返回的任何内容。

回调不符合标准的错误优先回调签名(function(error, result){}),但它应该仍然有效,尽管我认为没有任何方法可以检索第三个参数({{1 }})。

如果您在方法中执行此操作,则直接使用Future可能更合适。