Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save helabenkhalfallah/dec0dc75a752456e24f3dc72504dbdba to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/dec0dc75a752456e24f3dc72504dbdba to your computer and use it in GitHub Desktop.
Run to completion (Part 2)
const doLongOperation = (operation) => new Promise((resolve, reject) => {
if (operation) {
console.log('Lets resolve the operation');
resolve(operation);
} else {
console.log('Lets reject the operation');
reject(new Error("Empty operation !"));
}
});
const doOperations = async (x, y) => {
try {
const operation1 = await doLongOperation(x + y);
console.log('operation1 : ', operation1);
const operation2 = operation1 - y;
console.log('operation2 : ', operation2);
const operation3 = (operation1 * operation2) + 2 * (x - y);
console.log('operation3 : ', operation3);
return operation3;
} catch (error) {
// catches errors
console.log('error : ', error);
}
}
doOperations(3, 2)
.then(value => console.log('value : ', value));
console.log('Lets continue others staff');
/*
"Lets resolve the operation"
"Lets continue others staff"
"operation1 : ", 5
"operation2 : ", 3
"operation3 : ", 17
"value : ", 17
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment