promise-to-call
v0.1.0
Published
Replace callbacks with promises.
Downloads
6
Readme
Promise to Call
Replace callbacks with promises.
Targets to call can be methods of objects (useful "this") or functions (useless "this").
Installation
$ npm install promise-to-call
Usage
const promiseTo = require('promise-to-call');
promiseTo.$callMethod(someObject, 'someMethod', 2, 3)
.then((data) => { ... })
.catch((err) => { ... });
promiseTo.$callFunction(someFunction, 2, 3)
.then((data) => { ... })
.catch((err) => { ... });
Example
const AWS = require('aws-sdk');
const promiseTo = require('promise-to-call');
function whoAmI() {
const stsService = new AWS.STS({ region: 'us-east-1' });
return promiseTo.$callMethod(stsService, 'getCallerIdentity', {})
.then((data) => {
return data.Account;
})
.catch(console.error);
}
whoAmI().then(console.log); // -> 123456789012
Options
This module uses Promise
by default. If you want to use some other class, do this:
promiseTo.usePromiseClass(SomePromiseClass);
promiseTo.usePromiseClass(); // back to using Promise
Tests
$ npm test