callback-as-promised

0.0.1 • Public • Published

callback-as-promised

Don't make promises you can't keep.

Develop with promises and expose, both, a callback and a promise interface.

var callbackAsPromised = require('callback-as-promised');
 
var CourteousClient = function() {};
 
CourteousClient.prototype.doSomethingAsync = callbackAsPromised(function(successful) {
  var deferred = new Deferred();
  setTimeout(function() {
    if (successful) {
      deferred.resolve('very successful');
    } else {
      deferred.reject(new Error("wasn't successful"));
    }
  }, 500);
  return deferred.promise;
});
 
var courteousClient = new CourteousClient();
 
courteousClient.doSomethingAsync(true, function(err, result) {
  if (err) {
    // handle error
  }
  // handle success
});
 
courteousClient.doSomethingAsync(true).then(function(result) {
  // handle success
}, function(error) {
  // handle error
});

Readme

Keywords

none

Package Sidebar

Install

npm i callback-as-promised

Weekly Downloads

1

Version

0.0.1

License

Apache 2

Last publish

Collaborators

  • plinio-square
  • artemchubaryan
  • akolyte01
  • amoghk
  • jguze-sq
  • xiao-square
  • joanc
  • square-owner
  • adrw
  • square-developers
  • matahwoosh
  • hirokiosame
  • dpapastrat
  • maxbeatty
  • linnea
  • pretzelhammer
  • alexandersmanning
  • steckel