Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #814 from electron-userland/optional-sequential-hooks
Add a utility function to execute hooks serially
- Loading branch information
Showing
6 changed files
with
146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const pify = require('pify') | ||
|
||
module.exports = { | ||
promisifyHooks: function promisifyHooks (hooks, args) { | ||
if (!hooks || !Array.isArray(hooks)) { | ||
return Promise.resolve() | ||
} | ||
|
||
return Promise.all(hooks.map(hookFn => pify(hookFn).apply(this, args))) | ||
}, | ||
serialHooks: function serialHooks (hooks) { | ||
return function () { | ||
const args = Array.prototype.splice.call(arguments, 0, arguments.length - 1) | ||
const done = arguments[arguments.length - 1] | ||
let result = Promise.resolve() | ||
for (const hook of hooks) { | ||
result = result.then(() => hook.apply(this, args)) | ||
} | ||
|
||
return result.then(() => done()) // eslint-disable-line promise/no-callback-in-promise | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters