Created
November 14, 2016 19:50
-
-
Save iffy/b335b4a6135a182d597da5d830eafdb8 to your computer and use it in GitHub Desktop.
Poor man's yepnope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MinimalPromise() { | |
this.callbacks = []; | |
this.then = function(cb) { | |
this.callbacks.push(cb); | |
if (this.result !== undefined && !(this.result instanceof MinimalPromise)) { | |
this._pump(); | |
} | |
return this; | |
}.bind(this); | |
this.resolve = function(result) { | |
this.result = result; | |
this._pump(); | |
}.bind(this); | |
this._pump = function() { | |
while (this.callbacks.length) { | |
var cb = this.callbacks.shift(); | |
this.result = cb(this.result); | |
if (this.result instanceof MinimalPromise) { | |
this.result.then(function(result) { | |
this.result = result; | |
this._pump(); | |
}.bind(this)) | |
break | |
} | |
} | |
}.bind(this); | |
return this; | |
} | |
function loadSource(srcs, idx) { | |
idx = idx || 0; | |
var d = new MinimalPromise(); | |
var script_elem = document.createElement('script'); | |
script_elem.addEventListener('error', function(ev) { | |
loadSource(srcs, idx+1).then(d.resolve); | |
}, false); | |
script_elem.addEventListener('load', function(ev) { | |
d.resolve(srcs[idx]); | |
}, false); | |
script_elem.setAttribute('src', srcs[idx]); | |
document.body.appendChild(script_elem); | |
return d; | |
} | |
function all(promises) { | |
var results = []; | |
var resolved = 0; | |
var expected = promises.length; | |
var d = new MinimalPromise(); | |
for (var i = 0; i < expected; i++) { | |
var p = promises[i]; | |
p.then(function(result) { | |
results[i] = result; | |
resolved += 1; | |
if (resolved === expected) { | |
d.resolve(results); | |
} | |
}); | |
} | |
return d; | |
} | |
// | |
// Run a list of functions in parallel, returning | |
// a promise that will fire when they're all done. | |
// | |
function parallel(funcs) { | |
var dlist = []; | |
for (var i = 0; i < funcs.length; i++) { | |
dlist.push(funcs[i]()); | |
} | |
return all(dlist) | |
return d; | |
} | |
// | |
// Run a list of functions one after another, | |
// returning a promise that will | |
// fire when they're all done. | |
// | |
function sequentially(funcs) { | |
var d = new MinimalPromise(); | |
for (var i = 0; i < funcs.length; i++) { | |
d.then(function(funcs, i) { | |
return funcs[i](); | |
}.bind(this, funcs, i)); | |
} | |
d.resolve(true); | |
return d; | |
} | |
function l_parallel(funcs) { | |
return function() { | |
return parallel(funcs); | |
} | |
} | |
function l_sequentially(funcs) { | |
return function() { | |
return sequentially(funcs); | |
} | |
} | |
function l_loadSource(libs) { | |
return function() { | |
return loadSource(libs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment