2

Lets say I have a given function I can not change which has a callback function:

markAsRead(item_id,function(result){console.log(result)})

How can I wrap that function with a promise so I could know which item_id was returned? Something like this:

markRead(item_id).then(function(result)
    {
      emailOwner(result)
    },
    function(result)
    {
      emailSystem(result)
});
bymannan
  • 1,353
  • 2
  • 13
  • 23

1 Answers1

3
function markRead(item_id) {
    return new Promise(function(resolve, reject) {

      markAsRead(item_id,function(result){
          if (/* result is OK */) {
            resolve(result);
          }
          else {
            reject(result);
          }
      })
    });
}
lorefnon
  • 12,875
  • 6
  • 61
  • 93