is-generator.js 581 B

123456789101112131415161718192021222324252627282930
  1. /**
  2. * Export generator function checks.
  3. */
  4. module.exports = isGenerator
  5. module.exports.fn = isGeneratorFunction
  6. /**
  7. * Check whether an object is a generator.
  8. *
  9. * @param {Object} obj
  10. * @return {Boolean}
  11. */
  12. function isGenerator (obj) {
  13. return obj &&
  14. typeof obj.next === 'function' &&
  15. typeof obj.throw === 'function'
  16. }
  17. /**
  18. * Check whether a function is generator.
  19. *
  20. * @param {Function} fn
  21. * @return {Boolean}
  22. */
  23. function isGeneratorFunction (fn) {
  24. return typeof fn === 'function' &&
  25. fn.constructor &&
  26. fn.constructor.name === 'GeneratorFunction'
  27. }