chore: simplify promisify helper (#15952)

chore: simplify promisify helper
This commit is contained in:
Shelley Vohr 2018-12-10 08:13:09 -08:00 committed by GitHub
parent db2fda1b6f
commit 4b18a38e9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 23 deletions

View file

@ -69,23 +69,29 @@ const deprecate = {
})
},
promisify: (promise, cb) => {
const oldName = `function with callbacks`
const newName = `function with Promises`
promisify: (fn, cbParamIndex) => {
const fnName = fn.name || 'function'
const oldName = `${fnName} with callbacks`
const newName = `${fnName} with Promises`
const warn = warnOnce(oldName, newName)
if (typeof cb !== 'function') return promise
if (process.enablePromiseAPIs) warn()
return promise
.then(res => {
process.nextTick(() => {
cb(null, res)
return function (...params) {
const cb = params.splice(cbParamIndex, 1)[0]
const promise = fn.apply(this, params)
if (typeof cb !== 'function') return promise
if (process.enablePromiseAPIs) warn()
return promise
.then(res => {
process.nextTick(() => {
cb(null, res)
})
}, err => {
process.nextTick(() => {
cb(err)
})
})
}, err => {
process.nextTick(() => {
cb(err)
})
})
}
},
renameProperty: (o, oldName, newName) => {