Skip to content

Instantly share code, notes, and snippets.

@AliSawari
Created June 4, 2019 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliSawari/3a9f03049a37180258d921e8246c0971 to your computer and use it in GitHub Desktop.
Save AliSawari/3a9f03049a37180258d921e8246c0971 to your computer and use it in GitHub Desktop.
here's how to create promise-based functions
// say take a number and return its double
// resolve : the success call . which goes in the first then
// reject : the error call, which goes in .catch
function double(number){
return new Promise((resolve, reject) => {
if(typeof number == 'number'){
resolve(number * 2)
} else reject("the argument should a Number")
})
}
// outputs 8
double(4).then(d => console.log(d))
.catch(err => console.log(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment