Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Last active October 21, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffposnick/c09f2950bd4ec47f044f to your computer and use it in GitHub Desktop.
Save jeffposnick/c09f2950bd4ec47f044f to your computer and use it in GitHub Desktop.
Using Promises to wait for UI actions
function waitForLogin(message) {
return new Promise(function(resolve) {
if (alreadyLoggedIn) {
resolve();
} else {
if (message) {
showMessage(message);
}
addEventListener('logged-in', resolve);
}
});
}
// Scattered throughout the code.
waitForLogin("Login in to do something.").then(doSomething);
waitForLogin("Login to do something else.").then(doSomethingElse);
waitForLogin().then(doSomethingWithoutPrompting);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment