Skip to content

cebor/es-promisify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ES2015 Promisify Library

NPM version NPM downloads Build Status

This library enables node-styled functions to work as Promises through the use of ES2015 syntax.

Examples

Single-valued callback with multiple parameters

For a snipped like the following:

'use strict';

const Promisify = require('es-promisify');

// Test a promise that returns a single value
function sum (a, b, callback) {
    callback(null, a + b);
}

async function main () {
    let sumPromise = Promisify(sum);
    console.log(await sumPromise(2, 2));
}

main();

We'd expect to have an output like this one:

4

Multiple-valued output with multiple arguments

For a snippet like the following

'use strict';

const Promisify = require('es-promisify');

// Test return of multiple values
function multiOutput (a, b, callback) {
    callback(null, a, b);
}

async function main () {
    let multiOutputPromise = Promisify(multiOutput);
    console.log(await multiOutputPromise('a', 'b');
}

main();

We'd expect to have an output like this one:

['a', 'b']

Function bound to an object state

For a snippet like the following:

'use strict';

const Promisify = require('es-promisify');

// Test binding of methods inside objects
class Bindable {
    constructor () {
        this._prop = 3;
    }

    get prop () {
        return this._prop;
    }

    increment (callback) {
        this._prop += 1;
        callback(null, this.prop);
    }
}

async function main () {
    let bindable = new Bindable();
    let incrementPromise = Promisify(bindable.increment, bindable);
    console.log(await incrementPromise());
}

main();

We'd expect an output like this one:

4