Last active
October 21, 2015 16:56
-
-
Save jeffposnick/c09f2950bd4ec47f044f to your computer and use it in GitHub Desktop.
Using Promises to wait for UI actions
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 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); |
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
// test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment