universalify
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Universalify is a powerful npm package that enhances the functionality of callback- or promise-based functions to support both promises and callbacks. By leveraging JavaScript's native promise implementation, Universalify ensures wide-ranging compatibility and enhanced functionality.
Using Universalify is an intuitive process. Upon installing the package with 'npm install universalify', two primary methods can be utilized: 'universalify.fromCallback(fn)' and 'universalify.fromPromise(fn)'.
When dealing with callback-based functions, the 'fromCallback' method can be employed as follows:
const universalify = require('universalify');
function callbackFn(n, cb) {
setTimeout(() => cb(null, n), 15);
}
const fn = universalify.fromCallback(callbackFn);
This function can work with both Promises and Callbacks. For Promises, the usage would look like this:
fn('Hello World!')
.then(result => console.log(result))
.catch(error => console.error(error));
And for Callbacks:
fn('Hi!', (error, result) => {
if (error) return console.error(error);
console.log(result); // -> Hi!
});
For Promise-based functions 'fromPromise' method can be utilized:
const universalify = require('universalify');
function promiseFn(n) {
return new Promise(resolve => {
setTimeout(() => resolve(n), 15);
});
}
const fn = universalify.fromPromise(promiseFn);
The function can be used with Promises and Callbacks in a similar way as shown above.
The documentation for Universalify is primarily accessible on the GitHub page of the package. Simply visit 'https://github.com/RyanZim/universalify' to access detailed explanations, usage scenarios, and example codes. It's a comprehensive repository of knowledge for developers looking to get the most out of the Universalify package.