javascriptnode-promisify

Promisify a function which returns null


This function returns null and I need to promisify it. but I don't understand this code. Can anyone help with this.

return iyzipay.payment.create(req, function (err, result) {
    return('result:'+ result + 'error:'+ err);
})

Solution

  • Wrap your code in a Promise and call either resolve or reject once the operation is completed.

    return new Promise(function(resolve, reject) {
        iyzipay.payment.create(req, function (err, result) {
            if (err) {
                reject(err)
            } else {
                resolve(result)
            }
        })
    })