Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Understanding Promises in JavaScript
Pantelis Theodosiou
Pantelis Theodosiou

Posted on • Edited on • Originally published atpantelis.theodosiou.me

     

Understanding Promises in JavaScript

Photo byEmre Turkan onUnspash

Introduction

Modern web development relies heavily on asynchronous programming, which enables us to handle time-consuming operations effectively without delaying the completion of other tasks. Promises are a strong feature of JavaScript that make asynchronous operations easier to understand and improve code readability. The goal of this article is to give readers a thorough understanding of JavaScript promises, including information on their definition, lifecycle stages, helper functions, and inner workings.

What are Promises in JavaScript?

An asynchronous operation's eventual success (or failure) and the value it generates are represented by an object called a promise. Instead of nesting callbacks, it enables us to handle asynchronous code in a more elegant and organized way by chaining methods.

Promises can be in one of three states:pending,fulfilled, orrejected. The initial state of a Promise, which denotes that the asynchronous operation is still ongoing, is pending. If the operation succeeds, the Promise moves to the fulfilled state, and if there is a problem, it switches to the rejected state. "Settling" describes the process of going from pending to either fulfilled or rejected.

Promise Lifecycle Stages

Let's examine each phase of the lifecycle of a Promise in depth, with code examples:

Pending Stage

A Promise is in the pending state when it is created. The asynchronous operation is still ongoing at this point, and the Promise is neither accepted nor rejected.

Here's an example:

constpromise=newPromise((resolve,reject)=>{// Asynchronous operation (e.g., fetching data from an API)// resolve(result) or reject(error) will be called later});
Enter fullscreen modeExit fullscreen mode

Fulfilled Stage

The Promise goes into the fulfilled state once the asynchronous operation is successfully completed. At this point, the associated value (result) becomes available. To handle a fulfilled Promise, we use the.then() method.

Here's an example:

constpromise=newPromise((resolve,reject)=>{// Simulating an asynchronous operationsetTimeout(()=>{resolve("Operation succeeded!");},2000);});promise.then((result)=>{console.log(result);// Output: "Operation succeeded!"});
Enter fullscreen modeExit fullscreen mode

Rejected Stage

In the event that there is a problem with the asynchronous operation, the Promise goes into the rejected state. It denotes that the operation failed and provides an error object with pertinent information. To handle a rejected Promise, we use the.catch() method.

Here's an example:

constpromise=newPromise((resolve,reject)=>{// Simulating an asynchronous operationsetTimeout(()=>{reject(newError("Something went wrong!"));},2000);});promise.catch((error)=>{console.log(error.message);// Output: "Something went wrong!"});
Enter fullscreen modeExit fullscreen mode

Chaining Promises

Promises have a number of important benefits, including the ability to chain together several asynchronous operations, which improves code readability and prevents the dreaded "callback hell." We accomplish this by using the.then() method to return a newPromise.

Here's an example:

constgetUser=()=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous operationsetTimeout(()=>{resolve({id:1,name:"John"});},2000);});};constgetUserPosts=(user)=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous operationsetTimeout(()=>{resolve(["Post 1","Post 2"]);},2000);});};getUser().then((user)=>getUserPosts(user)).then((posts)=>console.log(posts));// Output: ["Post 1", "Post 2"]
Enter fullscreen modeExit fullscreen mode

Helper Functions for Promises

Along with the core methods that Promises provide, JavaScript also provides a number of helper functions that improve the functionality and readability of asynchronous code. Common tasks are made easier by these helper functions, which also improve control flow and error handling.

Promise.all()

When all of the Promises in the input array have been fulfilled, this function returns a new Promise that fulfills.

Here's an example:

constfetchUser=()=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous API call to fetch user datasetTimeout(()=>{constuser={id:1,name:"John"};resolve(user);},2000);});};constfetchPosts=()=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous API call to fetch user postssetTimeout(()=>{constposts=["Post 1","Post 2"];resolve(posts);},1500);});};constfetchComments=()=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous API call to fetch user commentssetTimeout(()=>{constcomments=["Comment 1","Comment 2"];resolve(comments);},1000);});};Promise.all([fetchUser(),fetchPosts(),fetchComments()]).then(([user,posts,comments])=>{console.log("User:",user);console.log("Posts:",posts);console.log("Comments:",comments);}).catch((error)=>{console.log("Error:",error);});
Enter fullscreen modeExit fullscreen mode

The three functionsfetchUser(),fetchPosts(), andfetchComments() are included in the example above. For user data, user posts, and user comments, each function simulates an asynchronous API call by returning a Promise.

By passing an array of Promises ([fetchUser(), fetchPosts(), fetchComments()]) toPromise.all(), we create a new Promise that fulfills once every Promise in the array has been successfully completed. When handling the fulfillment, the.then() method applies an array destructuring syntax to retrieve the resolved values of each Promise.

When all of the Promises are fulfilled in this situation, the array destructuring assigns the resolved values offetchUser(),fetchPosts(), andfetchComments() to the variablesuser,posts, andcomments respectively. The user, posts, and comments are then recorded to the console.

The.catch() method is called and the error is logged to the console ifany of the Promises fail.

Promise.all() allows us to efficiently fetch multiple asynchronous resources and handle them all at once after each request has successfully finished.

Promise.race()

As soon as any of the Promises in the input array settles, this function returns a new Promise that either fulfills or rejects.

Here's an example:

constfetchResource=(resource,delay)=>{returnnewPromise((resolve,reject)=>{// Simulating an asynchronous API call with a specified delaysetTimeout(()=>{resolve(`${resource} is fetched successfully in${delay}ms`);},delay);});};constresource1=fetchResource("Resource 1",2000);constresource2=fetchResource("Resource 2",1500);constresource3=fetchResource("Resource 3",1000);Promise.race([resource1,resource2,resource3]).then((result)=>{console.log(result);}).catch((error)=>{console.log(error);});
Enter fullscreen modeExit fullscreen mode

In the example above, there are three functions calledfetchResource() that simulate asynchronous API calls by returning Promises. Each call simulates the time it takes to fetch a particular resource by having a different delay time.

When an array of Promises ([resource1, resource2, resource3]]) is passed to thePromise.race() method, a new Promise is created that settles (fulfills or rejects) in response to any Promise in the array. The resolved value of the successful Promise is passed as theresult parameter and is logged to the console in the.then() method, which is used to handle fulfillment. In this case, the resource that resolves first (i.e., the one with the shortest delay) will be considered the winner, and its resolved value will be printed to the console.

The.catch() method is called and the error is logged to the console if any of the Promises fail.

We can run several asynchronous operations simultaneously and react to the outcome of the fastest one by using thePromise.race() method. It is beneficial in situations where we want to act in accordance with the initial response or completion.

Promise.resolve() and Promise.reject()

Without the need for additional asynchronous operations, these functions allow you to create Promises that have already been fulfilled or rejected, respectively.

Because of the strong capabilities these helper functions offer to manipulate and control Promises, JavaScript asynchronous programming is now more adaptable and expressive.

Promise.resolve() andPromise.reject() are used in the following example of code:

constfetchData=(shouldSucceed)=>{if(shouldSucceed){returnPromise.resolve("Data fetched successfully");}else{returnPromise.reject(newError("Failed to fetch data"));}};fetchData(true).then((result)=>{console.log(result);}).catch((error)=>{console.log(error);});fetchData(false).then((result)=>{console.log(result);}).catch((error)=>{console.log(error);});
Enter fullscreen modeExit fullscreen mode

ThefetchData() function in the example above has ashouldSucceed parameter that specifies whether the data fetch should be successful or unsuccessful.Promise.resolve() is used to create and return a Promise that immediately fulfills with the message "Data fetched successfully" ifshouldSucceed istrue.Promise.reject() is used to create and return a Promise that immediately rejects with a newError object and the message "Failed to fetch data" ifshouldSucceed isfalse.

The returned Promise is fulfilled in the first call tofetchData() withshouldSucceed set totrue and the fulfillment is managed by the.then() method. Theresult parameter is passed the resolved value "Data fetched successfully", which is then logged to the console.

In the second invocation offetchData() withshouldSucceed set tofalse, the returned Promise is rejected, and the.catch() method is used to handle the rejection. The error object containing the message "Failed to fetch data" is passed as theerror parameter and logged to the console.

By utilizingPromise.resolve() andPromise.reject(), we can easily create Promises that are already resolved or rejected without the need for additional asynchronous operations. This is useful when handling synchronous values or errors as Promises.

Conclusion

JavaScript's asynchronous programming has been revolutionized by promises, which offer a well-organized and beautiful way to handle laborious tasks. Promises help us avoid callback hell and create more readable, maintainable code.

The definition of promises, their lifecycle stages, and the function of helper functions likePromise.all(),Promise.race(),Promise.resolve(), andPromise.reject() were all covered in this extensive guide. We can handle asynchronous operations efficiently and gracefully handle success and error scenarios by understanding the lifecycle stages -pending,fulfilled, andrejected.

Developers can create dependable and effective applications that easily handle complex asynchronous tasks by utilizing Promises and their helper functions. Control flow is streamlined and code readability is improved by Promise chains. Additionally, JavaScript's helper functions improve error handling and streamline the execution of numerous asynchronous operations.

Modern web development requires the use of asynchronous programming, and becoming proficient with promises enables developers to produce code that is cleaner and easier to maintain. You can build robust applications that effectively handle asynchronous operations by integrating Promises into your JavaScript projects and utilizing their strength and helper functions.


If you found this post helpful or enjoyed it, consider supporting me bybuying me a coffee. Your support helps me create more valuable content. ☕ Thank you!

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
grantdotdev profile image
Grant Riordan
Lvl 11 - Lead Software Engineer & Lego FanaticTwitter link below 👇
  • Location
    Yorkshire, England
  • Education
    BSc(Hons) in Applied Computing
  • Work
    Application Development Specialist
  • Joined

Great article, explains the use of promises well.

CollapseExpand
 
ptheodosiou profile image
Pantelis Theodosiou
Web developer passionate about software engineering and coffee.
  • Location
    Thessaloniki, Greece
  • Education
    B.Sc in Computer Science
  • Work
    Software Developer at Athens Technology Center
  • Joined
• Edited on• Edited

Thank you! Glad you found it helpful!

CollapseExpand
 
grantdotdev profile image
Grant Riordan
Lvl 11 - Lead Software Engineer & Lego FanaticTwitter link below 👇
  • Location
    Yorkshire, England
  • Education
    BSc(Hons) in Applied Computing
  • Work
    Application Development Specialist
  • Joined

Apologies, I didn't utilise the content of the article myself. I am already familiar with the concept / usage.

I was more congratulating you on the well written explanation 👍 good job.

CollapseExpand
 
felistus profile image
Obieze Ezeugo Felistus
Frontend Developer [React.js, Vue.js]
  • Location
    Port Harcourt Nigeria
  • Education
    Federal University of Technology Owerri
  • Joined

Nice article. Good job!

CollapseExpand
 
dyaskur profile image
Muhammad Dyas Yaskur
  • Location
    Indonesia
  • Joined

Thanks to your article. Even though I had previously utilized JavaScript promises without a comprehensive understanding, your article has greatly enhanced my comprehension.

CollapseExpand
 
ptheodosiou profile image
Pantelis Theodosiou
Web developer passionate about software engineering and coffee.
  • Location
    Thessaloniki, Greece
  • Education
    B.Sc in Computer Science
  • Work
    Software Developer at Athens Technology Center
  • Joined

I'm happy to hear that my article helped you understand JavaScript promises better. Please feel free to ask any further questions you may have.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web developer passionate about software engineering and coffee.
  • Location
    Thessaloniki, Greece
  • Education
    B.Sc in Computer Science
  • Work
    Software Developer at Athens Technology Center
  • Joined

More fromPantelis Theodosiou

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp