Skip to content

Instantly share code, notes, and snippets.

@pixelsnob
Created September 16, 2018 19:42
Show Gist options
  • Save pixelsnob/17f784a403114068ab82a5cda8b9a7de to your computer and use it in GitHub Desktop.
Save pixelsnob/17f784a403114068ab82a5cda8b9a7de to your computer and use it in GitHub Desktop.
const pp = function() {
return new Promise((resolve, reject) => {
let r = (Math.round(Math.random() * 5, 0));
if (r > 3) {
setTimeout(() => resolve(r), r * 1000);
} else {
reject(`${r} is not enough`);
}
});
};
pp().then((r) => console.log(r))
.catch(err => console.error(err))
.finally(() => console.log('always runs'));
(async function() {
try {
let r = await pp();
console.log('await version: ' + r);
} catch (err) {
console.error('await error: ', err);
} finally {
console.log('await always runs');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment