0
0
Javascriptprogramming~20 mins

Promise chaining in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Promise chaining
📖 Scenario: Imagine you are ordering a pizza online. First, you select the pizza, then you pay for it, and finally, you get a confirmation message. Each step happens one after the other.
🎯 Goal: You will create three functions that return promises for each step: selectPizza, payForPizza, and confirmOrder. Then, you will chain these promises together to show the full order process.
📋 What You'll Learn
Create a function called selectPizza that returns a promise resolving to the string 'Pizza selected'.
Create a function called payForPizza that takes a string and returns a promise resolving to 'Payment done'.
Create a function called confirmOrder that takes a string and returns a promise resolving to 'Order confirmed'.
Chain the three functions using .then() to run them one after another.
Print the final confirmation message.
💡 Why This Matters
🌍 Real World
Promise chaining is used in real life when you need to do tasks one after another that take time, like loading data from the internet or processing payments.
💼 Career
Understanding promise chaining is important for web developers to handle asynchronous operations smoothly, improving user experience and app reliability.
Progress0 / 4 steps
1
Create the selectPizza function
Write a function called selectPizza that returns a promise which resolves to the string 'Pizza selected'.
Javascript
Need a hint?

Use new Promise and call resolve with the string 'Pizza selected'.

2
Create the payForPizza function
Write a function called payForPizza that takes one parameter called message and returns a promise which resolves to the string 'Payment done'.
Javascript
Need a hint?

The function takes message but you only need to resolve with 'Payment done'.

3
Create the confirmOrder function
Write a function called confirmOrder that takes one parameter called message and returns a promise which resolves to the string 'Order confirmed'.
Javascript
Need a hint?

Similar to the other functions, resolve with 'Order confirmed'.

4
Chain the promises and print the final message
Use selectPizza() and chain it with payForPizza and confirmOrder using .then(). Finally, print the result of confirmOrder using console.log.
Javascript
Need a hint?

Chain the functions with .then() and print the last result with console.log.