chore: fix promisify helper (#16544)

* chore: fix promise deprecation helper

* fix deprecations

* update deprecation tests
This commit is contained in:
Shelley Vohr 2019-01-25 14:23:24 -08:00 committed by GitHub
parent 63bf370cc0
commit 5a35c3a279
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 65 additions and 48 deletions

View file

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