codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

TypeScript

40 / 60 topics
39Async Programming in TypeScript40Promises41Async/Await
Tutorials/TypeScript/Promises
🔷TypeScript

Promises

Updated 2026-05-15
10 min read

Promises

Introduction

In the world of JavaScript, especially when dealing with asynchronous operations like fetching data from a server or reading files, we often need to handle tasks that don't complete immediately. Traditionally, callbacks were used for handling such scenarios, but they could lead to complex and hard-to-maintain code known as "callback hell." To address this, JavaScript introduced Promises, which provide a more elegant way to work with asynchronous operations.

Concept

A Promise is an object representing the eventual completion or failure of an asynchronous operation. It allows you to write cleaner and more readable code by chaining methods like .then() for successful operations and .catch() for handling errors.

States of a Promise

  1. Pending: The initial state, neither fulfilled nor rejected.
  2. Fulfilled: The operation completed successfully.
  3. Rejected: The operation failed.

Examples

Let's dive into some practical examples to understand how Promises work.

Basic Example

Here's a simple example of creating and using a Promise:

TypeScript
1const myPromise = new Promise((resolve, reject) => {
2setTimeout(() => {
3 resolve("Success!");
4}, 1000);
5});
6
7myPromise
8.then(result => console.log(result)) // Output: Success!
9.catch(error => console.error(error));

In this example, we create a Promise that resolves after 1 second with the message "Success!". We then use .then() to handle the resolved value and .catch() to handle any potential errors.

Chaining Promises

Promises can be chained together to perform multiple asynchronous operations in sequence:

TypeScript
1const fetchData = () => {
2return new Promise((resolve, reject) => {
3 setTimeout(() => {
4 resolve("Data fetched successfully!");
5 }, 1000);
6});
7};
8
9fetchData()
10.then(result => {
11 console.log(result); // Output: Data fetched successfully!
12 return "Processing data...";
13})
14.then(processedResult => {
15 console.log(processedResult); // Output: Processing data...
16})
17.catch(error => console.error(error));

In this example, fetchData returns a Promise that resolves after 1 second. We chain .then() to handle the resolved value and perform additional operations.

Handling Errors

It's important to handle errors properly using .catch():

TypeScript
1const fetchData = () => {
2return new Promise((resolve, reject) => {
3 setTimeout(() => {
4 reject("Failed to fetch data!");
5 }, 1000);
6});
7};
8
9fetchData()
10.then(result => console.log(result))
11.catch(error => console.error(error)); // Output: Failed to fetch data!

In this example, fetchData returns a Promise that rejects after 1 second. We use .catch() to handle the error and log it.

What's Next?

Now that you have a good understanding of Promises, you can explore more advanced topics like async/await, which provide an even more readable way to work with asynchronous code. This will be covered in the next section.

Feel free to experiment with Promises in your projects to see how they can simplify handling asynchronous operations!


PreviousAsync Programming in TypeScriptNext Async/Await

Recommended Gear

Async Programming in TypeScriptAsync/Await