index.js 836 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var co = require('co')
  2. exports = module.exports = deferImmediate
  3. exports.defer =
  4. exports.immediate =
  5. exports.setImmediate = deferImmediate
  6. function createCallback(ctx, gen, cb) {
  7. return function () {
  8. cb = cb || error
  9. co.call(ctx, gen).then(function () {
  10. cb()
  11. }, cb)
  12. }
  13. }
  14. function deferImmediate(gen, cb) {
  15. return setImmediate(createCallback(this, gen, cb))
  16. }
  17. exports.nextTick = function deferNextTick(gen, cb) {
  18. return process.nextTick(createCallback(this, gen, cb))
  19. }
  20. exports.timeout =
  21. exports.setTimeout = function deferTimeout(gen, timeout, cb) {
  22. return setTimeout(createCallback(this, gen, cb), timeout)
  23. }
  24. exports.interval =
  25. exports.setInterval = function deferInterval(gen, timeout, cb) {
  26. return setInterval(createCallback(this, gen, cb), timeout)
  27. }
  28. function error(err) {
  29. if (err) throw err
  30. }