Promiseifyish Examples

Example uses of the Promiseifyish library

Sample JavaScript Code

/**
* A function to demonstrate callback behavior.
*
* @param {boolean} success if true, the success callback will be invoked
* @param {*} value the value to pass to the callback
* @param {Function} [successCallback] the success callback
* @param {Function} [failureCallback] the failure callback
*/
function someFunction(success, value, successCallback, failureCallback) {
    if (success && 'function' === typeof successCallback) {
        successCallback(value);
    } else if (!success && 'function' === typeof failureCallback) {
        failureCallback(value);
    }
}

// Standard usage:
someFunction(true, 'some value',
    value => console.log('standard', 'success', value),
    error => console.error('standard', 'failure', error)
);

// After promisification:
let promisifiedFunction = Promiseify(someFunction);
// Invoke as a promise
promisifiedFunction(true, 'some value')
    // 'value' is an array of all the parameters which the callback would
    // normally have been executed with
    .then(value => console.log('standard', 'success', value))
    .catch(error => console.error('promisified', 'failure', error));