如何在客户端上使用Meteor.wrapAsync?

Dav*_*len 6 meteor

在我的Meteor客户端代码中,我正在尝试使用仅具有异步调用的第三方API.如何在客户端上使用Meteor.wrapAsync以同步方式调用此API?文档似乎表明这是可能的:http: //docs.meteor.com/#/full/meteor_wrapasync

以下是我想以同步方式调用的一些示例代码:

var func1 = function(callback) {
  Meteor.setTimeout(function() {
    console.log('func1 executing');
    callback(null, {success: true});
  }, 2000);
};

var func2 = function(callback) {
  Meteor.setTimeout(function() {
    console.log('func2 executing');
    callback(null, {success: true});
  }, 1000);
};

var wrapped1 = Meteor.wrapAsync(func1);
var wrapped2 = Meteor.wrapAsync(func2);

Template.test.rendered = function() {
    wrapped1();
    console.log('After wrapped1()');
    wrapped2();
    console.log('After wrapped2()');
};
Run Code Online (Sandbox Code Playgroud)

目前,这产生了这个输出:

After wrapped1()
After wrapped2()
func2 executing
func1 executing
Run Code Online (Sandbox Code Playgroud)

我希望它能产生:

func1 executing    
After wrapped1()
func2 executing
After wrapped2()
Run Code Online (Sandbox Code Playgroud)

我已将此代码放入MeteorPad:http://meteorpad.com/pad/fLn9DXHf7XAACd9gq/Leaderboard

Aks*_*hat 2

Meteor.wrapAsync为了同构代码而在客户端工作。这样您就可以编写可以在客户端和服务器上共享的代码,而不会出现 Meteor 崩溃或抱怨的情况。

客户端上不可能有同步代码。至少不使用并非所有浏览器都可用的 ES6 功能。

正如 saeimunt Meteor.wrapAsync 的评论中那样,需要在客户端进行回调。