0% found this document useful (0 votes)
2 views

Practice Sheet_ Asynchronous Callbacks and Promises

The document outlines upcoming session schedules for practice sheet discussions and doubt sessions on January 23rd and 27th, respectively. It includes a series of programming problems focused on asynchronous callbacks and Promises, detailing tasks for reading files, chaining callbacks, converting callbacks to Promises, and handling errors. The problems aim to enhance understanding of asynchronous programming in Node.js.

Uploaded by

dhruvb2028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practice Sheet_ Asynchronous Callbacks and Promises

The document outlines upcoming session schedules for practice sheet discussions and doubt sessions on January 23rd and 27th, respectively. It includes a series of programming problems focused on asynchronous callbacks and Promises, detailing tasks for reading files, chaining callbacks, converting callbacks to Promises, and handling errors. The problems aim to enhance understanding of asynchronous programming in Node.js.

Uploaded by

dhruvb2028
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Upcoming Session Schedules

●​ 23rd January [Macro Campus]

○​ 2:30 PM - 4:30 PM [Practice Sheet Discussion]

●​ 27th January [Micro Campus]

○​ 5 PM - 7 PM: Doubt Session

Practice Sheet: Asynchronous Callbacks and Promises

Problem 1: Read a File with Callbacks

Description: Write a program that reads a file using asynchronous callbacks and logs its
content. Goal: Understand how to use callbacks for asynchronous operations. Tasks:

1.​ Use fs.readFile from the Node.js fs module.


2.​ Handle success and error cases within the callback.

Problem 2: Chain Multiple Callbacks

Description: Fetch data from two APIs sequentially using callbacks. Goal: Understand callback
chaining. Tasks:

1.​ Use the request module or fetch.


2.​ Fetch data from the first API, then use its response to call the second API.

Problem 3: Convert Callbacks to Promises

Description: Refactor a function that uses callbacks into a function that uses Promises. Goal:
Learn how to create and use Promises. Tasks:

1.​ Take an example callback-based function.


2.​ Rewrite it to return a Promise.
Problem 4: Handle Multiple Promises

Description: Fetch data from three APIs concurrently and log their results. Goal: Understand
Promise.all. Tasks:

1.​ Use Promise.all to fetch data concurrently.


2.​ Log results or errors.

Problem 5: Sequential Execution with Promises

Description: Execute two asynchronous tasks sequentially using Promises. Goal: Practice
chaining .then(). Tasks:

1.​ Write two asynchronous functions that return Promises.


2.​ Chain them to execute sequentially.

Problem 6: Error Handling in Promises

Description: Simulate a failing API call and handle errors. Goal: Learn error handling with
.catch(). Tasks:

1.​ Create a Promise that rejects.


2.​ Handle the rejection with .catch().

Problem 7: Timeout with Promises

Description: Create a Promise that resolves after a delay. Goal: Learn to use setTimeout
with Promises. Tasks:

1.​ Write a function that returns a Promise.


2.​ Use setTimeout to resolve the Promise after 2 seconds.
Problem 8: Nested Promises

Description: Simulate a scenario with nested Promises. Goal: Understand how to manage
nested asynchronous flows. Tasks:

1.​ Create a Promise that resolves to another Promise.


2.​ Handle the nested Promises correctly.

Problem 9: Promisify a Callback Function

Description: Convert a Node.js callback function to return a Promise. Goal: Practice manual
Promisification. Tasks:

1.​ Use fs.readFile with a callback.


You might also like