benjie / node-simple-aws

A simple wrapper around AWS's official aws-sdk Node.JS module
1 stars 1 forks source link

Feature: Promises #2

Open konsumer opened 4 years ago

konsumer commented 4 years ago

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, just await). If there is interest, I can implement it.

konsumer commented 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
konsumer commented 4 years ago

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.