Skip to content

eugeneware/co-spawn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

co-spawn

setImmediate for the co generator framework

build status

Calling setImmediate in a co-friendly way is painful and you have to launch co() in your setImmediate function. This is a simple wrapper.

Installation

This module is installed via npm:

$ npm install co-spawn

Example Usage

Non-blocking spawn

var spawn = require('co-spawn');
co(function *() {
  var c = { counter: 0 };

  // this will run first
  c.counter++;

  // put the following code into another turn of the event loop
  spawn(function *() {
    yield finish(c, done);
  });

  // this will run second
  c.counter++;
})();

function *finish(c, cb) {
  // this will run last
  c.counter++;

  expect(c.counter).to.equal(3);
  done();
}

Blocking spawn

If for some reason you want to block on the spawn, just yield:

var spawn = require('co-spawn');
co(function *() {
  var c = { counter: 0 };

  // this will run 1st
  c.counter++;

  // put the following code into another turn of the event loop, but block
  yield spawn(function *() {
    yield finish(c, done);
  });

  // this will run last
  c.counter++;
})();

function *finish(c, cb) {
  // this will run 2nd
  c.counter++;

  expect(c.counter).to.equal(2);
  done();
}

About

setImmediate for the co generator framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published