Skip to content

Instantly share code, notes, and snippets.

@kevinpschaaf
Created August 27, 2014 21:20
Show Gist options
  • Save kevinpschaaf/1989b7dd7024a83b7fe0 to your computer and use it in GitHub Desktop.
Save kevinpschaaf/1989b7dd7024a83b7fe0 to your computer and use it in GitHub Desktop.
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