chore: fix promisify helper (#16544)
* chore: fix promise deprecation helper * fix deprecations * update deprecation tests
This commit is contained in:
parent
63bf370cc0
commit
5a35c3a279
12 changed files with 65 additions and 48 deletions
|
@ -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))
|
||||
})
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue