Skip to content

Instantly share code, notes, and snippets.

@antono
Last active January 28, 2018 17:58
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 antono/12f2e4604784dd35e515f9bcbe1801e0 to your computer and use it in GitHub Desktop.
Save antono/12f2e4604784dd35e515f9bcbe1801e0 to your computer and use it in GitHub Desktop.
Promise vs async/await
async function doSomething (value, reject = true) {
if (reject) {
throw value;
} else {
return value;
}
}
async function task() {
try {
await doSomething('one', true);
} catch (err) {
return err;
}
try {
await doSomething('one', false);
} catch (err) {
return err;
}
}
task().then(console.log);
const doSomething = (value, reject = true) => {
if (reject) {
return Promise.reject(value)
} else {
return Promise.resolve(value)
}
}
doSomething('one', true)
.then(
() => doSomething('two', false),
(err) => console.log(err) // handle errors from doSomething('one', true)
)
.catch(console.log); // handle errors from doSomething('two', false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment