0.1.2 • Published 6 years ago

promise-support v0.1.2

Weekly downloads
19
License
MIT
Repository
github
Last release
6 years ago

promise-support

Promises + Callbacks support wrapper

Why?

In situation when function should support both callbacks and Promises, this is simple wrapper which gives this out of the box

Usage

const supportsPromise = require('promise-support');
const myFn = (arg1, arg2, arg3, callback) => {
  setImmediate(() => {
    callback(null, arg1, arg2, arg3);
  });
};

const wrappedFn = supportsPromise(myFn);
wrappedFn(1, 2, 3, (err, first, second, third) => {
	//do smth old-styled callbacks way
});
// OR use promises:
wrappedFn(1, 2, 3)
.then(res => {/*do smth with result*/})
.catch(err => {/*do smth with err*/});
// OR async:
try {
	const result = await wrappedFn(1, 2, 3);
	// do smth with result
} catch (e) {
	// do smth with error
}

Promise result wrapping

  • Original function passes only 1 parameter => promise will get this parameter when resolved.
  • Original function passes multiple parameters => promise will array with parameters.
0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

7 years ago

0.0.1

7 years ago