Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Created January 16, 2016 09:57
Show Gist options
  • Save khanghoang/c6b7b15598527e230aad to your computer and use it in GitHub Desktop.
Save khanghoang/c6b7b15598527e230aad to your computer and use it in GitHub Desktop.
let waitsInProgress = [];
let message = `Timeout, it seems we don have the response from the async function`;
let waitFor = (testFunc, done, timeLeft = 2000) => {
waitsInProgress.push(setTimeout(() => {
if (timeLeft <= 0) {
done(new Error(message));
} else if (testFunc()) {
done();
} else {
waitFor(testFunc, done, timeLeft - 10);
}
}, 10));
};
waitFor.clear = () => waitsInProgress.map(clearTimeout); //optionally call this in the beforeEach to ensure rogue tests are not still waiting
export default waitFor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment