hooks.js 732 B

1234567891011121314151617181920212223242526
  1. 'use strict'
  2. const pify = require('pify')
  3. module.exports = {
  4. promisifyHooks: function promisifyHooks (hooks, args) {
  5. if (!hooks || !Array.isArray(hooks)) {
  6. return Promise.resolve()
  7. }
  8. return Promise.all(hooks.map(hookFn => pify(hookFn).apply(this, args)))
  9. },
  10. serialHooks: function serialHooks (hooks) {
  11. return function () {
  12. const args = Array.prototype.splice.call(arguments, 0, arguments.length - 1)
  13. const done = arguments[arguments.length - 1]
  14. let result = Promise.resolve()
  15. for (const hook of hooks) {
  16. result = result.then(() => hook.apply(this, args))
  17. }
  18. return result.then(() => done()) // eslint-disable-line promise/no-callback-in-promise
  19. }
  20. }
  21. }