Created
August 27, 2014 21:20
-
-
Save kevinpschaaf/1989b7dd7024a83b7fe0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
window.asyncSeries = function(series, callback, forwardExceptions) { | |
series = series.slice(); | |
var next = function(err) { | |
if (err) { | |
if (callback) { | |
callback(err); | |
} | |
} else { | |
var f = series.shift(); | |
if (f) { | |
if (!forwardExceptions) { | |
f(next); | |
} else { | |
try { | |
f(next); | |
} catch(e) { | |
if (callback) { | |
callback(e); | |
} | |
} | |
} | |
} else { | |
if (callback) { | |
callback(); | |
} | |
} | |
} | |
}; | |
next(); | |
}; | |
window.waitFor = function(fn, next, intervalOrMutationEl, timeout, timeoutTime) { | |
timeoutTime = timeoutTime || Date.now() + (timeout || 1000); | |
intervalOrMutationEl = intervalOrMutationEl || 32; | |
try { | |
fn(); | |
} catch (e) { | |
if (Date.now() > timeoutTime) { | |
throw e; | |
} else { | |
if (isNaN(intervalOrMutationEl)) { | |
intervalOrMutationEl.onMutation(intervalOrMutationEl, function() { | |
waitFor(fn, next, intervalOrMutationEl, timeout, timeoutTime); | |
}); | |
} else { | |
setTimeout(function() { | |
waitFor(fn, next, intervalOrMutationEl, timeout, timeoutTime); | |
}, intervalOrMutationEl); | |
} | |
return; | |
} | |
} | |
next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment