JSFiddle - React, Tailwind, and code Playground

by Gajus Kuizinas

JavaScript

var foo,
    curry,
    controller;

foo = function (parameters, callback) {
    setTimeout(function () {
        callback(parameters);
    }, 100);
};

curry = function (method) {
    var args = Array.prototype.slice.call(arguments, 1);
 
    return function (callback) {
        args.push(callback);
 
        return method.apply({}, args);
    };
};

controller = function (generator) {
    var iterator;

    iterator = generator();

    function advancer (response) {
        var state;

        if (response && response.error) {
            return iterator.throw(response.error);
        }

        state = iterator.next(response);

        if (!state.done) {
            state.value(advancer);
        }
    }

    advancer();
};

controller(function * () {
    var a,
        b,
        c;

    try {
        a = yield curry(foo, 'a');
        b = yield curry(foo, {error: 'Something went wrong.'});
        c = yield curry(foo, 'c');
    } catch (e) {
        console.log(e);
    }

    console.log(a, b, c);
});

// Something went wrong.
// a undefined undefined