Skip to content

Instantly share code, notes, and snippets.

@JohnnyMcFadden
Last active November 30, 2020 17:22
Show Gist options
  • Save JohnnyMcFadden/b15ff4388e61438527a0cd3bb79a4ca3 to your computer and use it in GitHub Desktop.
Save JohnnyMcFadden/b15ff4388e61438527a0cd3bb79a4ca3 to your computer and use it in GitHub Desktop.
The Joe Codes Blog - Promises --> Creating a new promise
// "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