Created
June 4, 2019 13:41
-
-
Save AliSawari/3a9f03049a37180258d921e8246c0971 to your computer and use it in GitHub Desktop.
here's how to create promise-based functions
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
// 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