Skip to content

Instantly share code, notes, and snippets.

@pmogollons
Last active May 10, 2021 15:21
Show Gist options
  • Save pmogollons/da7fbb7f0f54928a46c378dff11dea5c to your computer and use it in GitHub Desktop.
Save pmogollons/da7fbb7f0f54928a46c378dff11dea5c to your computer and use it in GitHub Desktop.
async function startCallbackHell() {
try {
const res1 = await callback1(params);
const res2 = await callback2(res1);
const res3 = await callback3(res2);
const res4 = await callback4(res3);
const res5 = await callback5(res4);
} catch (error) {
// Do something with the error
} finally {
// Do somthing after the try is executed, even if it gets an error
}
}
const callback1 = async (params) => {
// Actually do something
return 'res1';
}
const callback2 = async (params) => {
// Actually do something
return 'res2';
}
const callback3 = async (params) => {
// Actually do something
return 'res3';
}
const callback4 = async (params) => {
// Actually do something
return 'res4';
}
const callback5 = async (params) => {
// Actually do something
return 'res5';
}
@JonJairo
Copy link

Why do you use const and not var or let ?

@pmogollons
Copy link
Author

Values are constant and are not updated later in the code, you should use let only when the value of the variable is going to be updated in upcoming code and try to never use var.

@JonJairo
Copy link

Values are constant and are not updated later in the code, you should use let only when the value of the variable is going to be updated in upcoming code and try to never use var.

Understandable :D Thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment