How to use fibers - 10 common examples

To help you get started, we’ve selected a few fibers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github meteor / promise / test / tests.js View on Github external
it("should not double-wrap callbacks", Promise.async(function () {
    // Consume all fibers currently in the pool, so that we can detect how many
    // new fibers are created after that point.
    var done = new Future();
    var origCount = Fiber.fibersCreated;

    while (Fiber.fibersCreated == origCount) {
      // Force creation of a new fiber that blocks until we're done.
      var ready = new Future();
      Promise.asyncApply(function () {
        ready.return();
        done.wait();
      });
      ready.wait();  // Make sure fiber is running before we continue.
    }

    // OK, we're now in a situation where a Promise.then() should create
    // *one* new fiber.
    var baseCount = Fiber.fibersCreated;

    // Create some named no-op promises and callbacks. I'm assigning names
    // to these so that the comments below are easier to read.
    var promise1 = Promise.resolve();
github meteor / promise / test / tests.js View on Github external
it("should not double-wrap callbacks", Promise.async(function () {
    // Consume all fibers currently in the pool, so that we can detect how many
    // new fibers are created after that point.
    var done = new Future();
    var origCount = Fiber.fibersCreated;

    while (Fiber.fibersCreated == origCount) {
      // Force creation of a new fiber that blocks until we're done.
      var ready = new Future();
      Promise.asyncApply(function () {
        ready.return();
        done.wait();
      });
      ready.wait();  // Make sure fiber is running before we continue.
    }

    // OK, we're now in a situation where a Promise.then() should create
    // *one* new fiber.
    var baseCount = Fiber.fibersCreated;

    // Create some named no-op promises and callbacks. I'm assigning names
github meteor / promise / test / tests.js View on Github external
var done = new Future();
    var origCount = Fiber.fibersCreated;

    while (Fiber.fibersCreated == origCount) {
      // Force creation of a new fiber that blocks until we're done.
      var ready = new Future();
      Promise.asyncApply(function () {
        ready.return();
        done.wait();
      });
      ready.wait();  // Make sure fiber is running before we continue.
    }

    // OK, we're now in a situation where a Promise.then() should create
    // *one* new fiber.
    var baseCount = Fiber.fibersCreated;

    // Create some named no-op promises and callbacks. I'm assigning names
    // to these so that the comments below are easier to read.
    var promise1 = Promise.resolve();
    var promise2 = Promise.resolve();
    var returnPromise2 = function () { return promise2; };
    var cb = function () {};

    // Make sure this didn't create any fibers.
    assert.strictEqual(baseCount, Fiber.fibersCreated);

    // This should create one fiber, and return it to the pool.
    // Note that you can remove these two lines and the test still works and
    // tests the right thing. This is just checking my assumptions.
    promise1.then(returnPromise2).await();
    assert.strictEqual(baseCount + 1, Fiber.fibersCreated);
github meteor / promise / test / tests.js View on Github external
// Create some named no-op promises and callbacks. I'm assigning names
    // to these so that the comments below are easier to read.
    var promise1 = Promise.resolve();
    var promise2 = Promise.resolve();
    var returnPromise2 = function () { return promise2; };
    var cb = function () {};

    // Make sure this didn't create any fibers.
    assert.strictEqual(baseCount, Fiber.fibersCreated);

    // This should create one fiber, and return it to the pool.
    // Note that you can remove these two lines and the test still works and
    // tests the right thing. This is just checking my assumptions.
    promise1.then(returnPromise2).await();
    assert.strictEqual(baseCount + 1, Fiber.fibersCreated);

    // This should NOT create a another fiber. It should reuse the fiber
    // created by the above block. However, if callback double-wrapping
    // is not prevented, then cb will end up being wrapped *twice*, and
    // thus *two* fibers will be created at the same time.
    //
    // What happens is:
    // * .then(cb) wraps cb (let's call the wrapped version wcb) and passes
    //   it on to the Promise implementation.
    // * On next tick, promise1 "completes", so returnPromise2() is called.
    // * Since it returns a promise (promise2), the Promise implementation
    //   calls promise2.then(wcb) -- forwarding the callback on to the next
    //   promise in the chain.
    // * Our monkey-patched .then() is used here. If we don't detect that
    //   the callback is already wrapped, we'll end up wrapping it again!
    var promise3 = promise1.then(returnPromise2);
github laverdet / node-fibers / test / fibonacci.js View on Github external
function Fibonacci() {
	return Fiber.prototype.run.bind(Fiber(function() {
    Fiber.yield(0); // F(0) -> 0
    var prev = 0, curr = 1;
    while (true) {
      Fiber.yield(curr);
      var tmp = prev + curr;
      prev = curr;
      curr = tmp;
    }
  }));
}
github ybogdanov / node-sync / benchmarks / index.js View on Github external
var start = new Date();
        for(var i = 0; i <= max; i++) {
            sumAsync(3, 4);
        }
        var asyncTime = new Date - start;
        console.log('async() took %d ms (x%d)', asyncTime, ~~ (asyncTime / nativeTime));

        var start = new Date();
        for(var i = 0; i <= max; i++) {
            sumAsync.sync(null, 3, 4);
        }
        var asyncSyncTime = new Date - start;
        console.log('async().sync() took %d ms (x%d)', asyncSyncTime, ~~ (asyncSyncTime / nativeTime));

        var Future = require('fibers/future');
        var sumFuture = Future.wrap(sum);
        var start = new Date();
        for(var i = 0; i <= max; i++) {
            Future.wait(sumFuture(3, 4));
        }
        var fibersFutureTime = new Date - start;
        console.log('Fibers.future took %d ms (x%d)', fibersFutureTime, ~~ (fibersFutureTime / nativeTime));

        // Test Fibers
        Fiber(function(){
            var f = Fiber.current;
            var start = new Date();
            for(var i = 0; i <= max; i++) {
                sum(3, 4, function() {
                    f.run();
                });
                Fiber.yield();
github erelsgl / nlu-server / research / ppdb / evalmeasure_5ed_comp.js View on Github external
_.each(utils.subst(generated), function(sub, key1, list1){
          console.log("original seed")
          console.log(or)
          console.log("from ppdb")
          console.log(generated)
          console.log("substring")
          console.log(sub)
          utils.recursionredis([sub], [1], function(err,actual) {
            fiber.run(actual)
          })
          var paraphr = Fiber.yield()

          utils.onlycontent(sub, function (err,strcontent){
            fiber.run(utils.elimination(strcontent))
          })
          var paraphrcontent = Fiber.yield()

          console.log("length of paraphrases")
          console.log(paraphr.length)

          console.log("content part")
          console.log(paraphrcontent)

          var score = paraphrcontent.length==1? 1: Math.pow(paraphr.length, paraphrcontent.length)
          console.log("score")
          console.log(score)

          dist.push(score)
        // console.log(paraphr)
        }, this)
      }, this)
github erelsgl / nlu-server / research / ppdb / evalmeasure_5ed_grams.js View on Github external
var seeds = ['offer']
	var report = {}
  var FN = []

  console.log("number of seeds "+seeds.length)
  console.log(seeds)
		
  // retrieve all generated paraphases to the seeds
  utils.recursionredis(seeds, [1,1], function(err,actual) {
    console.log("number of PPDB paraphrases " + actual.length)
    utils.clusteration(_.unique(actual), function(err, clusters){
      fiber.run(clusters)
    })
  })

  var clusters = Fiber.yield()
     
  console.log("number of clusters "+clusters.length)
  console.log(clusters)
  
  console.log("number of turns " + turns.length)
  _.each(turns, function(turn, key, list){
    if (key%10 == 0) console.log(key)
    if (turn['status'] == "active")
    if ('intent_keyphrases_rule' in turn)
      _.each(turn['intent_keyphrases_rule'], function(keyphrase, intent, list){ 
          if ((keyphrase != 'DEFAULT INTENT') && (keyphrase != '') 
            // && (keyphrase.indexOf("") == -1) && (keyphrase.indexOf("") == -1)
             )
            {

              keyphrase = keyphrase.replace("", "")
github erelsgl / nlu-server / research / ppdb / evalmeasure_5ed_comp.js View on Github external
var seeds = ['offer']
	var report = {}
  var FN = []

  console.log("number of seeds "+seeds.length)
  console.log(seeds)
		
  // retrieve all generated paraphases to the seeds
  utils.recursionredis(seeds, [1,1], function(err,actual) {
    console.log("number of PPDB paraphrases " + actual.length)
    utils.clusteration(_.unique(actual), function(err, clusters){
      fiber.run(clusters)
    })
  })

  var clusters = Fiber.yield()
     
  console.log("number of clusters "+clusters.length)
  console.log(clusters)
  
  console.log("number of turns " + turns.length)
  _.each(turns, function(turn, key, list){
    if (key%10 == 0) console.log(key)
    if (turn['status'] == "active")
    if ('intent_keyphrases_rule' in turn)
      _.each(turn['intent_keyphrases_rule'], function(keyphrase, intent, list){ 
          if ((keyphrase != 'DEFAULT INTENT') && (keyphrase != '') 
            // && (keyphrase.indexOf("") == -1) && (keyphrase.indexOf("") == -1)
             )
            {

              keyphrase = keyphrase.replace("", "")
github jpaulm / jsfbp / script / reader.js View on Github external
function myReadFile(path, options, proc) {    
    console.log('read started: ' + proc.name);
    fs.readFile(path, options, function(err, data) {
      //fbp.setCurrentProc(proc);
      console.log('callback for: ' + proc.name); 
      fbp.queueCallback(proc, data);
      // fiber.run(data);
       }); 
   console.log('read pending: ' + proc.name);   
   //console.log('yielded: ' + proc.name ); 
   return Fiber.yield();
  }