Last active
July 18, 2017 08:03
-
-
Save mindc/89e44d54b549d49ed490a9a10a815cf4 to your computer and use it in GitHub Desktop.
Javascript Promise - run stuff until user is logged in - mixin Promises with API code
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
isLoggedIn().then(main); // run main() when user logged in | |
let jsonrpc = new JSONRPC(); // JSONRPC client, Promise based | |
function isLoggedIn() { | |
return jsonrpc.call('User.isLoggedIn').catch(showLoginForm); | |
} | |
function logIn(args) { | |
return jsonrpc.call('User.logIn', args).catch(showLoginForm); | |
} | |
function showLoginForm(error) { | |
return new Promise(resolve => { | |
let mbox = new MessageBox( //API module, not promising, event based | |
`Username: <input name="username"/><br/> | |
Password: <input name="password" type="password"/>`, | |
`ERROR: ${error.message}` | |
); | |
mbox.actionEvent.attach(MB.IDOK, (msgbox, formData) => logIn(formData).then(resolve)); | |
mbox.actionEvent.attach(MB.IDCLOSE, () => showLoginForm(new Error('Close button not allowed')).then(resolve)); | |
mbox.show(); | |
}) | |
} | |
function main(data) { //data - return value of isLoggedIn/logIn function | |
// do important stuff | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment