mhlavacka
mhlavacka

Reputation: 701

Nested Async Functions - Future return running inside Fibers

I need to run two nested async functions and return callback from the second one to the client. future.return doesn't work inside Fibers. How to return result to the client without using collections?

Meteor.methods({
    'youtube':function(object) {

    var youTube = new YouTube();

    youTube.search(object.song, 1, function(error, result) {
      if (error) {
        console.log(error);
      }
      else {
        Fiber(function() {

          var future = new Future();

          ytdl.getInfo(result.url, function(err, result) {
            future.return({data: result});
          });

          return future.wait();

        }).run();

      }
    });
});

Upvotes: 1

Views: 383

Answers (1)

dr.dimitru
dr.dimitru

Reputation: 2702

Future should be returned in first method scope. And read about Meteor.bindEnvironment

var Future = Npm.require('fibers/future');
var bound  = Meteor.bindEnvironment(function(callback){ return callback(); });
Meteor.methods({
  'youtube':function(object) {
    var fut = new Future();
    var youTube = new YouTube();

    youTube.search(object.song, 1, function (error, result) {
      bound(function () {
        if (error) {
          console.log(error);
        } else {
          ytdl.getInfo(result.url, function(err, result) {
            fut.return({data: result});
          });
        }
      });
    });
    return fut.wait();
  }
});

Upvotes: 2

Related Questions