Topics: What is a Promise, Constructing a Promise Object, The Node setTimeout() Function, Consuming Promises, The onFulfilled and onRejected Functions, Using catch() with Promises, Chaining Multiple Promises, Avoiding Common Mistakes, Using Promise.all()
Project Title: Washing Dishes
Create a html file with your name like john.html
Add current code to that file:
<!DOCTYPE html> <html> <body> <script> // your code here </script> </body> </html>
- Create function
wash(resolve, reject)and use
setTimeoutto implements this logic after 1 second:
numDishesand set it to a random integer number between 1 to 10.
Wash: Passand resolve with
- Else class reject with
Create a new promise with
thenafter the promise to get the
numDishesand pass it to a new function named
dryfunction create a new promise and inside it use setTimeout with 2 seconds.
Inside setTimeout check
numDishesand if it is less then 3, then log
Dry: Passand resolve(numDishes), else reject with
thenafter previous then in step 5 and get
"Done " + numDishes + " Dishes."
If there is an error log "Sorry" and log the exact error into console.
Run this program multiple times to get different results.
Want to get reviewed?
Send Pull Request. Check how to deliver your code: https://codingwithbasir.com/how-to-deliver-projects/