Skip to content

Instantly share code, notes, and snippets.

@reoxb
Created November 19, 2020 20:57
Show Gist options
  • Save reoxb/2863aafdfd73075e4860bc8967c70902 to your computer and use it in GitHub Desktop.
Save reoxb/2863aafdfd73075e4860bc8967c70902 to your computer and use it in GitHub Desktop.
How promises works
function asyncFunction(x) {
return new Promise(function (resolve, reject) {
if(typeof x === 'number' && !isNaN(x)){
resolve( x * x)
} else {
reject(new Error('It\'s not a number'))
}
})
}
asyncFunction(10)
.then((result)=> console.log(result))
.catch((error)=> console.error(error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment