Last active
November 30, 2020 17:22
-
-
Save JohnnyMcFadden/b15ff4388e61438527a0cd3bb79a4ca3 to your computer and use it in GitHub Desktop.
The Joe Codes Blog - Promises --> Creating a new promise
This file contains 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
// "Producing code"... may be an asynchronous task that takes some length of time | |
const someValidator = (value) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (typeof value !== 'string') { | |
reject('Error: Value is not a string!'); | |
} else { | |
resolve('Success: Value was a string', value); | |
} | |
}, 1000); | |
}); | |
}; | |
// "Consuming Code"... that must wait for a fulfilled Promise | |
someValidator('some string') | |
.then((value) => console.log(value)) /* code if successful */ | |
.catch((value) => console.error(value)); /* code if some error */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment