Mailspring/packages/nylas-core/promise-utils.js
Juan Tejada 0c900b072d Add new relic to all pkgs + Revert to PromiseUtils methods
- PromiseUtils does not conflict with newrelics changes to Promise
- Other misc fixes
2016-07-13 19:08:24 -07:00

56 lines
1.3 KiB
JavaScript

/* eslint no-restricted-syntax: 0 */
require('promise.prototype.finally')
const _ = require('underscore')
global.Promise.prototype.thenReturn = function thenReturn(value) {
return this.then(function then() { return Promise.resolve(value); })
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
function each(iterable, iterator) {
return Promise.resolve(iterable).then((array) => {
return new Promise((resolve, reject) => {
Array.from(array).reduce((prevPromise, item, idx, len) => (
prevPromise.then(() => Promise.resolve(iterator(item, idx, len)))
), Promise.resolve())
.then(() => resolve(iterable))
.catch((err) => reject(err))
})
})
}
function promisify(nodeFn) {
return function wrapper(...fnArgs) {
return new Promise((resolve, reject) => {
nodeFn.call(this, ...fnArgs, (err, ...results) => {
if (err) {
reject(err)
return
}
resolve(...results)
});
})
}
}
function promisifyAll(obj) {
for (const key in obj) {
if (!key.endsWith('Async') && _.isFunction(obj[key])) {
obj[`${key}Async`] = promisify(obj[key])
}
}
return obj
}
module.exports = {
each,
sleep,
promisify,
promisifyAll,
props: require('promise-props'),
}