Open konsumer opened 4 years ago
The API can actually stay the same (with added promise support) with a bit of trickery testing the last param to see if it's a function.
For example:
await ddb.putItem('TEST', { id: 'TEST' })
in ddb.putItem
, check if typeof cb === 'function'
, and if it is use that as callback, otherwise use a generated promise-callback for underlying client-call.
const promise = new Promise()
if (typeof cb !== 'function'){
cb = (err, ret) => err ? promise.reject(err) : promise.resolve(ret)
}
// ... later
return promise
I ended up just wrapping every this.method
with promisify
as it maintains backward-compatibility, and will keep the API mostly the same, with minimal effort.
I'd love it if this returned promises, instead of requiring a callback. This is fairly simple with util.promisify. This would also make the async unit-tests really nice & simple (no
await new Promise
, justawait
). If there is interest, I can implement it.