What do fibers / future really do?

What does the line of code below do?

Npm.require('fibers/future');

I looked online for examples, and I came across several such:

Future = Npm.require('fibers/future');
var accessToken = new Future();

What will be a variable in this case accessToken?

+4
source share
1 answer

The question is a bit old, but my 2 cents:

As Molda stated in a comment, the future main goal is to make async work synchronously. futureAn instance has 3 methods:

  • future.wait() basically tells your thread to basically pause until it is prompted to resume.
  • future.return(value), future, , , , , , const ret = future.wait(), ret - .
  • future.throw(error), , .

javascript , . Meteor , Meteor.method , . Promises, Meteor , , , .

:

Meteor.methods({
  foo: function() {
    const future = new Future();
    someAsyncCall(foo, function bar(error, result) {
      if (error) future.throw(error);
      future.return(result);
    });
    // Execution is paused until callback arrives
    const ret = future.wait(); // Wait on future not Future
    return ret;
  }
});
+6

All Articles