0

I use dynogels which is an framework to wrap AWS dynamoDB table, I use it to delete my item on the dynamoDB Table, the function they provide is callback functions.

https://github.com/clarkie/dynogels //document

// Destroy model using hash and range key
BlogPost.destroy('foo@example.com', 'Hello World!', function (err) { 
console.log('post deleted')
});    

But I would like to use promise instead of callback, then I am thinking can I put the Promise.resolve in the callback?

BlogPost.destroy('foo@example.com', 'Hello World!', Promise.resolve())
.then(()=>{
    console.log('post deleted')
});

How do I make an callback to return promise? I can't think anyway can do it so far.

ckky1213
  • 321
  • 5
  • 14

1 Answers1

0

There is a simple util for that, namely util.promisify https://nodejs.org/api/util.html#util_util_promisify_original

let util = require('util')

// create a copy and call it yourself ...
util.promisify(BlogPost.destroy).call(BlogPost, 'foo@example.com', 'Hello World!')
.then(()=>{  
  console.log('post deleted')
});



// ... or create a alternative method:
BlogPost.prototype.destroyPromises = util.promisify(BlogPost.destroy)

// later
BlogPost.destroyPromises('foo@example.com', 'Hello World!')
.then(()=>{  
  console.log('post deleted')
});
Bellian
  • 2,009
  • 14
  • 20