2

Within a function that is already within Meteor.binEnvironment, when I run <collection>.find ({}), I get the error throw new Error ('Can \' t wait without a fiber '); If you place that call also within Meteor.bindEnvironment(<collection>.find ({})), the error message becomes: throw new Error (noFiberMessage);

The function in question runs through Meteor.methods ({}) Where am I going wrong?

Example to reproduce the error:

Meteor.methods({
  "teste" : Meteor.bindEnvironment(function(){
    var Future = Meteor.require('fibers/future');
    var future = new Future();
    setTimeout(function(){
      return future.return(Sessions.findOne({}))
    }, 15000);
    console.log('fut', future.wait());
  })
});
rogeriojlle
  • 1,046
  • 1
  • 11
  • 19
  • I don't know if this solves your actual problem, but in your example, there's no need to use `Meteor.bindEnvironment`, just use `Meteor.setTimeout` instead of `setTimeout` (`Meteor.setTimeout` will use `Meteor.bindEnvironment` under the hood for you). – Peppe L-G Mar 03 '14 at 06:31

2 Answers2

1

Try using Meteor._wrapAsync instead.

This is an example of an async function, but any other would do:

var asyncfunction = function(callback) {
    Meteor.setTimeout(function(){
        callback(null, Sessions.findOne({}))
    }, 15000);
}

var syncfunction = Meteor._wrapAsync(asyncfunction);

var result = syncfunction();

console.log(result);

You could wrap any asynchronous function and make it synchronous and bind the fibers with it this way.

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • I could not apply the suggested solution in my project, currently do this way: `Meteor.methods({ "teste" : Meteor.bindEnvironment(function(){ var Fiber = Meteor.require('fibers'); var Future = Meteor.require('fibers/future'); var future = new Future(); setTimeout(function(){ return future.return( Fiber(function(){ Sessions.findOne({}); }).run() ); }, 15000); console.log('fut', future.wait()); }) });` – rogeriojlle Mar 10 '14 at 02:48
1

I could not apply the suggested solution in my project, currently do this way:

Meteor.methods({
  "teste" : Meteor.bindEnvironment(function(){
    var Fiber = Meteor.require('fibers');
    var Future = Meteor.require('fibers/future');
    var future = new Future();
    setTimeout(function(){
      return future.return(
        Fiber(function(){
          Sessions.findOne({});
        }).run()
      );
    }, 15000);
    console.log('fut', future.wait());
  })
});
rogeriojlle
  • 1,046
  • 1
  • 11
  • 19