Last active
May 12, 2020 09:33
-
-
Save kwebble/2bb4102037c33527810e810d4cbfa1d5 to your computer and use it in GitHub Desktop.
Example of the process flow to get a value from a JavaScript function that returns a Promise
This file contains hidden or 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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<script> | |
function action () { | |
console.log('ACTION') | |
return 'VALUE' | |
} | |
function getData () { | |
console.log('getData >>>') | |
let p = new Promise((resolve, reject) => { | |
window.setTimeout( | |
() => { | |
console.log('TIMEOUT') | |
resolve(action()) | |
}, | |
1000) | |
}) | |
console.log('<<< getData') | |
return p | |
} | |
getData().then(val => { | |
console.log('RESULT: ' + val) | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment