JSFiddle - React, Tailwind, and code Playground

by Kato Richardson

JavaScript

/**
 * {function} fx a function that may or may not return a promise
 * {jQuery.Deferred} def the deferred object to fulfill when `fx` completes
 * {object} scope the `this` to use inside `fx` (null if static)
 * {array} args any args to pass into `fx`
 * {*} prevValue result from previous step, undefined for the first step
 */

function(def, scope, args, prevValue) {
    try {
        // execute the function and evaluate it
        var v = fx.apply(scope, [prevValue].concat(args));
        if (v instanceof jQuery.Deferred) {
            // returned a promise, wait for it to resolve
            v.then(function() {
                def.resolve.apply(def, arguments);
            }, function() {
                def.reject.apply(def, arguments);
            });
        }
        else if (v instanceof Error) {
            // fx returned an Error, so we know something went wrong
            def.reject(v);
        }
        else {
            // no errors thrown and not a promise, resolve immediately
            def.resolve(v);
        }
    }
    catch (e) {
        // fx threw an Error, so we reject
        def.reject(e);
    }
}