How to use the fibers/future.wrap function in fibers

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 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 isaacs / rimraf / fiber.js View on Github external
// fiber/future port originally written by Marcel Laverdet
// https://gist.github.com/1131093
// I updated it to bring to feature parity with cb version.
// The bugs are probably mine, not Marcel's.
// -- isaacs

var path = require('path')
  , fs = require('fs')
  , Future = require('fibers/future')

// Create future-returning fs functions
var fs2 = {}
for (var ii in fs) {
  fs2[ii] = Future.wrap(fs[ii])
}

// Return a future which just pauses for a certain amount of time

function timer (ms) {
  var future = new Future
  setTimeout(function () {
    future.return()
  }, ms)
  return future
}

function realish (p) {
  return path.resolve(path.dirname(fs2.readlink(p)))
}
github enyojs / ares-project / hermes / node_modules / rimraf / fiber.js View on Github external
// fiber/future port originally written by Marcel Laverdet
// https://gist.github.com/1131093
// I updated it to bring to feature parity with cb version.
// The bugs are probably mine, not Marcel's.
// -- isaacs

var path = require('path')
  , fs = require('fs')
  , Future = require('fibers/future')

// Create future-returning fs functions
var fs2 = {}
for (var ii in fs) {
  fs2[ii] = Future.wrap(fs[ii])
}

// Return a future which just pauses for a certain amount of time

function timer (ms) {
  var future = new Future
  setTimeout(function () {
    future.return()
  }, ms)
  return future
}

function realish (p) {
  return path.resolve(path.dirname(fs2.readlink(p)))
}
github jtblin / syncho / benchmarks / simple.js View on Github external
require('sync');
      console.time('sync');
      for (i = 0; i < n; i++) {
        results.push(asyncFn.sync(null, i));
      }
      console.timeEnd('sync');

      require('fibrous');
      console.time('fibrous');
      for (i = 0; i < n; i++) {
        results.push(asyncFn.sync(i));
      }
      console.timeEnd('fibrous');

      var Fiber = require('fibers'), Future = require('fibers/future'), futureAsync = Future.wrap(asyncFn);
      console.time('fibers');
      var fiber = Fiber.current;
      for (i = 0; i < n; i++) {
        setTimeout(function() {
          fiber.run('foo'+i);
        }, t);
        results.push(Fiber.yield());
      }
      console.timeEnd('fibers');

      console.time('future');
      for (i = 0; i < n; i++) {
        results.push(futureAsync(i).wait());
      }
      console.timeEnd('future');
github jtblin / syncho / benchmarks / future.js View on Github external
for (var i = 0; i < n; i++) {
      batch[i].wait();
    }
    console.timeEnd('syncho-future');

    console.time('async.parallel');
    for (var i = 0, batch = []; i < n; i++) {
      batch.push(runAsync(i));
    }
    async.parallel(batch, function (err, res) {
      for (var i = 0; i < n; i++)
        res[i];
      console.timeEnd('async.parallel');
    });

    var Future = require('fibers/future'), wait = Future.wait, future = Future.wrap(asyncFn);
    console.time('future');
    for (var i = 0, batch = []; i < n; i++) {
      batch.push(future(i));
    }
    wait(batch);
    console.timeEnd('future');

  });
})();
github jtblin / syncho / benchmarks / object.js View on Github external
}
      console.timeEnd('fibrous');

      var Fiber = require('fibers');
      results = [];
      console.time('fibers');
      var fiber = Fiber.current;
      for (var i = 0; i < n; i++) {
        fs.stat('./package.json', function (err, res) {
          fiber.run(res);
        });
        results.push(Fiber.yield());
      }
      console.timeEnd('fibers');

      var Future = require('fibers/future'), futureAsync = Future.wrap(fs.stat.bind(fs), 1);
      results = [];
      console.time('future');
      for (var i = 0; i < n; i++) {
        results.push(futureAsync('./package.json').wait());
      }
      console.timeEnd('future');

      results = [];
      console.time('async.series');
      async.series(asyncSeries, function (err, res) {
        results = res;
        console.timeEnd('async.series');

        trueAsync();
      });
github andreypopp / react-async / index.js View on Github external
if (Fiber === undefined ||
        Fiber.current === undefined ||
        Fiber.current.__reactAsyncStatePacket === undefined) {
      return {};
    }

    invariant(
      typeof this.getInitialStateAsync === 'function',
      this.displayName + ' component must implement a `getInitialStateAsync` method. ' +
      'Otherwise you should not use ReactAsync.Mixin'
    );

    var Future = require('fibers/future');

    var getInitialStateAsync = Future.wrap(function(cb) {
      var promise = this.getInitialStateAsync(cb);

      if (promise && typeof promise.then === 'function') {
        promise.then(cb.bind(cb, null), cb);
      } else if (promise === false) {
        cb(null);
      }
    }.bind(this));
    var asyncState = getInitialStateAsync().wait();
    var fingerprint = getComponentFingerprint(this);

    var storedAsyncState = asyncState;

    if (typeof this.stateToJSON === 'function') {
      storedAsyncState = this.stateToJSON(storedAsyncState);
    }
github Picolab / pico-engine / packages / node-pico-engine-core / src / toFiberSync.js View on Github external
module.exports = function(fn){
    var fnF = Future.wrap(fn);
    return function(){
        return fnF.apply(this, arguments).wait();
    };
};
github Picolab / pico-engine / src / toFiberSync.js View on Github external
module.exports = function(fn){
  var fnF = Future.wrap(fn);
  return function(){
    return fnF.apply(this, arguments).wait();
  };
};
github hackhat / selenium-sync / src / utils.js View on Github external
utils.wrapPromise = function(fn, target, multi, isPrototype){
    return Future.wrap(function(){
        var args = Array.prototype.slice.call(arguments);
        var cb   = args.pop();
        /**
         * If you don't want to bind on target, such as prototype binding then
         * set it to true to bind to this. This will be the instance of the target
         * because is on it's prototype.
         */
        fn.apply(isPrototype ? this : target, args).then(function onSuccess(){
            var args = Array.prototype.slice.call(arguments);
            args.unshift(void 0);
            cb.apply({}, args);
        }, function onError(err){
            cb(err);
        })
    }, !!multi);
}