assured

Combine promises and callbacks together.
:cloud: Installation
$ npm i --save assured
:clipboard: Example
const assured = require("assured");
let foo = (age, cb) => {
if (typeof age === "function") {
cb = age;
}
cb = assured(cb);
if (typeof age !== "number") {
return cb(new Error("Invalid age."));
}
setTimeout(() => {
cb(null, `The provided age is ${age}`);
}, 100);
return cb._;
}
foo(err => console.log(err));
foo(42, (err, data) => console.log(err, data));
foo().then(x => {
console.log("Success: ", x);
}).catch(e => {
console.log("Error: ", e);
});
foo(42).then(x => {
console.log("Success: ", x);
}).catch(e => {
console.log("Error: ", e);
});
:memo: Documentation
assured(fn, p)
Proxies the callback function.
Params
- Function
fn
: The callback function to proxy. - Promise
p
: A custom promise constructor (default: the built-in Promise
).
Return
- Function The proxied callback function extended with:
resolver
(Function): The promise resolver.assuredResolve
(Function): The resolve method.assuredReject
(Function): The reject method._
(Promise): The promise object (used to return
from your function).
:yum: How to contribute
Have an idea? Found a bug? See how to contribute.
:moneybag: Donations
Another way to support the development of my open-source modules is
to set up a recurring donation, via Patreon. :rocket:
PayPal donations are appreciated too! Each dollar helps.
Thanks! :heart:
:dizzy: Where is this library used?
If you are using this library in one of your projects, add it in this list. :sparkles:
fn-result
—Take an (a)sync function result and pass it forward.scrape-it
—A Node.js scraper for humans.
:scroll: License
MIT © Ionică Bizău