-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-promise-reject-30-05-2022.js
30 lines (25 loc) · 1.13 KB
/
js-promise-reject-30-05-2022.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Rejecting a promise can happen for many reasons. For example, if we have a network error, or if we have a problem with the data we are getting from the server.
// Let's imagine such a scenario: We have an order for dumplings, but we fried them instead of steaming them like the order says.
// We only have a recipe for fried dumplings.
const dumplingsRecipe = () =>
new Promise((resolve) => {
setTimeout(() => {
resolve("Fried dumplings🥟");
}, 1000);
});
// We now receive an order for boiled dumplings:
const steamedDumplingsOrder = () =>
new Promise((resolve, reject) => {
// We have to cook our dumplings with our only recipe.
dumplingsRecipe().then((dumplings) => {
// If we cook our dumplings correctly, we can resolve the promise.
if (dumplings === "Steamed dumplings🥟") {
resolve(dumplings);
// If we cook our dumplings incorrectly, we can reject the promise.
} else {
reject("❌ This is not the dumplings I ordered.");
}
});
});
steamedDumplingsOrder().catch(console.error);
// Twitter reference: https://twitter.com/TommiEng/status/1531331593704554496