Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active December 28, 2017 07:38
Show Gist options
  • Save velizarn/8244d01fa3cd8a00063d467532eb7bb3 to your computer and use it in GitHub Desktop.
Save velizarn/8244d01fa3cd8a00063d467532eb7bb3 to your computer and use it in GitHub Desktop.
JavaScript basic Promise example
let getName = () => {
return new Promise((resolve, reject) => {
let name = "John Doe";
try {
setTimeout (() => {
resolve( name )
}, 1500)
} catch (e) {
return reject(e + '')
}
})
}
getName().then((result) => {
console.log(result)
}).catch((e) => {
console.error(e + '')
})
/**
* https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it
* https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises
* https://stackoverflow.com/questions/40576232/node-js-converting-module-functions-from-callbacks-to-promises-with-bluebird
* https://stackoverflow.com/questions/45630134/how-to-synchronize-functions-with-promises-in-node-js
* https://promisesaplus.com/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment