I may be missing something really obvious here, but how do I use util.promisify with a function which looks like this?
function awkwardFunction (options, data, callback) {
    // do stuff ...
    let item = "stuff message"
    return callback(null, response, item)
}
Which I can call like this:
 awkwardFunction ({doIt: true},'some data', (err, result, item) => {
      console.log('result')
      console.log(result)
      console.log('item')
      console.log(item)
      done()
    })
And get back
result
{ data: 'some data' }
item
stuff message
When using the promisified version:
let kptest = require('util').promisify(awkwardFunction)
kptest({doIt: true},'some data')
   .then((response, item) => {
    console.log('response')
    console.log(response)
    console.log('item')
    console.log(item)
 })
 .catch(err => {
     console.log(err)
  })
and trying to access both "response" and "item", it seems the 2nd param is ignored...
result
{ data: 'some data' }
item
undefined
Is there a way to do this WITHOUT changing the function (in reality, it is a library function, so I can't).
The util. promisify() method basically takes a function as an input that follows the common Node. js callback style, i.e., with a (err, value) and returns a version of the same that returns a promise instead of a callback.
promisify() function is not available. The idea is to create a new Promise object that wraps around the callback function. If the callback function returns an error, we reject the Promise with the error. If the callback function returns non-error output, we resolve the Promise with the output.
The util. promisify() method defines in utilities module of Node. js standard library. It is basically used to convert a method that returns responses using a callback function to return responses in a promise object.
util.promisify is intended to be used with Node-style callbacks with function (err, result): void signature.
Multiple arguments can be treated manually:
let kptest = require('util').promisify(
  (options, data, cb) => awkwardFunction(
    options,
    data,
    (err, ...results) => cb(err, results)
  )
)
kptest({doIt: true},'some data')
.then(([response, item]) => {...});
In case more sophisticated functionality is wanted, some third-party solution like pify can be used instead of util.promisify, it has multiArgs option to cover this case.
You could make your own promisify, where you return a promise that resolves with the arguments of the callback and on the then block you destructure them. Hope this helps.
function awkwardFunction (options, data, callback) {
    // do stuff ...
    let item = "stuff message";
    return callback(null, data, item);
}
const mypromisify = (fn) =>
    (...args) =>
        new Promise(resolve =>
            fn(...args,
                (...a) => resolve(a)
            )
        );
const kptest = mypromisify(awkwardFunction);
kptest({ doIt: true }, 'some data')
    .then(([error, response, item]) => {
        console.log(response);
        console.log(item);
    })
    .catch(err => {
        console.log(err);
    });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With