Files
Verso/services/web/app/src/util/promises.js
T
Eric Mc Sweenandsharelatex 869fcf7952 Merge pull request #2089 from overleaf/em-mailchimp-unsubscribe
Handle error on Mailchimp unsubscribe when deleting users

GitOrigin-RevId: 8923480e6d50de45003fd7741610f995753a412b
2019-09-03 15:25:01 +00:00

32 lines
930 B
JavaScript

const { promisify } = require('util')
module.exports = { promisifyAll }
/**
* Promisify all functions in a module.
*
* This is meant to be used only when all functions in the module are async
* callback-style functions.
*
* It's very much tailored to our current module structure. In particular, it
* binds `this` to the module when calling the function in order not to break
* modules that call sibling functions using `this`.
*
* This will not magically fix all modules. Special cases should be promisified
* manually.
*/
function promisifyAll(module, opts = {}) {
const { without = [] } = opts
const promises = {}
for (const propName of Object.getOwnPropertyNames(module)) {
if (without.includes(propName)) {
continue
}
const propValue = module[propName]
if (typeof propValue === 'function') {
promises[propName] = promisify(propValue).bind(module)
}
}
return promises
}