es-nodeifydeprecated

Nodeify extension for native ECMAScript 2015 promises

Usage no npm install needed!

<script type="module">
  import esNodeify from 'https://cdn.skypack.dev/es-nodeify';
</script>

README

es-nodeify

npm version

Simple extension to ECMAScript 2015 (ES6) promises. It provides Promise with a nodeify() prototype function, similar to what various promise libraries offer.

It gives your library's promise-based asynchronous functions a way to handle an optional callback function(error, result) parameter, effectively turning it into a classic Node.js style asynchronous function. When the callback is omitted, it returns a Promise, as if nodeify() was never called.

Please note that es-nodeify (as the prefix suggests) works with the ECMAScript 2015 Promise prototype only. Either make sure you're using native ECMAScript 2015, or use a compliant Promise library.

Installation

Install with npm:

npm install --save es-nodeify

Usage

require('es-nodeify');

module.exports = function(callback) {
    return new Promise(function(resolve, reject) {
        // ...
    }).nodeify(callback);
};

You can then use the above module in 2 ways:


let someFunction = require('./my-function.js');

someFunction().then(
    function(result) {
        // Handle result
    }, function(error) {
        // Handle error
    }
);

someFunction(function(error, result) {
    if (error) {
        // Handle error
        return;
    }

    // Handle result
});

License

Published under the MIT License.