genify.js 571 B

123456789101112131415161718192021222324252627282930
  1. /*!
  2. * node-thunkify-wrap - genify.js
  3. * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
  4. * MIT Licensed
  5. */
  6. 'use strict';
  7. /**
  8. * Module dependencies.
  9. */
  10. module.exports = function createGenify(thunkify) {
  11. return function genify(fn, ctx) {
  12. if (isGeneratorFunction(fn)) {
  13. return fn;
  14. }
  15. function* genify() {
  16. var thunk = thunkify(fn);
  17. return yield thunk.apply(ctx || this, arguments);
  18. }
  19. return genify;
  20. };
  21. };
  22. function isGeneratorFunction(fn) {
  23. return typeof fn === 'function' && fn.constructor.name === 'GeneratorFunction';
  24. }