Skip to content

Instantly share code, notes, and snippets.

@kwebble
Last active May 12, 2020 09:33
Show Gist options
  • Save kwebble/2bb4102037c33527810e810d4cbfa1d5 to your computer and use it in GitHub Desktop.
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
<!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