JSFiddle - React, Tailwind, and code Playground

by Scott Kaye

JavaScript

console.clear();

var async = function (imports, callback) {
    //First index of imports is the function to run
    //All other indices are variables to forcefully import into the scope
    var i, len, code = imports[0].toString();
    for (i = 1, len = imports.length; i < len; ++i) {
        var value = JSON.stringify(eval(imports[i]));
        code = code.replace(imports[i], value);
    }

    var url = URL.createObjectURL(new Blob(
    ["var r=(", code, ")();postMessage(r)"], {
        type: "application/javascript"
    }));

    var worker = new Worker(url);
    worker.onmessage = function (e) {
        callback(e.data);
    };

    URL.revokeObjectURL(url);
};

var nums = [1, 2, 3, 4, 5];

async([function () {
    return nums.reduce(function (p, c) {
        return p + c;
    });
}, "nums"], function (result) {
    console.log(result);
});