Frontend Development: JavaScript Promises

Peter Jeon·2023년 6월 24일
0

Frontend Development

목록 보기
28/80
post-custom-banner

JavaScript Promises

In this blog, we will dive deep into the world of JavaScript Promises. Promises are a powerful feature of JavaScript that allow us to handle asynchronous operations. Before we get into the details, let's compare synchronous and asynchronous operations.

SynchronousAsynchronous
Operations are performed one at a time and only when the previous operation has completed.Operations can be performed concurrently, without waiting for the previous operation to complete.
Can lead to blocking I/O which can negatively impact performance.Non-blocking I/O, leads to better utilization of system resources.

Now, let's dive into the details of JavaScript Promises.

What is a Promise?

Promise Concept

A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. Essentially, it's a returned object to which you attach callbacks, instead of passing callbacks into a function.

A Promise is in one of these states:

  • Pending: initial state, neither fulfilled nor rejected.
  • Fulfilled: meaning that the operation completed successfully.
  • Rejected: meaning that the operation failed.

A pending promise can either be fulfilled with a value or rejected with a reason (error). When either of these options happens, the associated handlers queued up by a promise's then method are called.

Creating a Promise

Creating Promise

A Promise is created with the new keyword and its constructor. This constructor takes as its argument a function, called the 'executor function'. This function should take two functions as parameters. The first of these functions (resolve) is called when the asynchronous task completes successfully and returns the results of the task as a value. The second (reject) is called when the task fails, and returns the reason for failure, which is typically an error object.

Here's an example of a promise:

let promise = new Promise(function(resolve, reject) {
  // executor (the producing code, 'singer')
});

The functions resolve and reject are predefined by the JavaScript engine. So we don’t need to create them. Instead, we should write the executor to call them when ready.

Consuming a Promise

Consuming Promise

To use a promise, you must attach the then method to the promise object. then is called when the Promise is resolved and the Promise's value is passed to the then method's callback function. then also returns a Promise, allowing you to chain additional then calls.

To catch an error, you attach the catch method to the promise object. catch is called when the Promise is rejected and the reason for the rejection is passed to the catch method's callback function.

Here's an example of consuming a promise:

promise.then(function(result) {
  // handle a successful result
}, function(error) {
  // handle an error
});

The then function takes two arguments, a callback for a success case, and another for the failure case. Both are optional, so you can add a callback for the success or failure case only.

Conclusion

Conclusion

In this blog, we have learned about JavaScript Promises. We started with understanding the difference between synchronous and asynchronous operations. We then dived into what promises are and how they can be created and consumed. Promises in JavaScript provide a powerful way to handle asynchronous operations, allowing us to write cleaner, more readable, and maintainable code.

Promises are not just a fad, they're here to stay and are a part of the ECMAScript 6 specification. So, understanding them thoroughly will be beneficial for every JavaScript developer.

profile
As a growing developer, I am continually expanding my skillset and knowledge, embracing new challenges and technologies
post-custom-banner

1개의 댓글

comment-user-thumbnail
2023년 12월 12일

Great read on JavaScript Promises! If you're diving deeper into frontend development, you might find this guide on hiring JavaScript developers super helpful: https://www.cleveroad.com/blog/hire-javascript-developer/. It offers valuable insights for building a top-notch team.

답글 달기