Articles

JavaScript Promises And Fetch API Uses Guide

by Jay Dee AndolaSoft
JavaScript is an interpreted language where codes are executed line by line. But the execution will not wait for the dependent codes to be executed before you execute the next line. 

To achieve this feature JavaScript introduces the callback function. Basically this is associated with the asynchronous operations in JavaScript. But the issue with the callback function is if we have more than one asynchronous operation running at the same time. So it became hell to manage the code using the callback functions. 

The problems are

  • Hard to understand the codes because the code becomes lengthier and nested structure. 
  • Hard to manage the codes, because it is not clear which callbacks are called when and also there are so many callbacks to write to perform a particular task.
  • Also need not satisfy all the requirements

Here JavaScript introduces the concept of Promises. 

Promises are JavaScript objects that represent an eventual completion or failure of an asynchronous operation. Promises either resolved or rejected. That means it will either return success on resolve or error on rejection. 

In case of multiple asynchronous operations it provides chaining of Promises to handle that. So the code here is understandable and managed. Here is some example of callbacks and promises for your understanding.



Sponsor Ads


About Jay Dee Advanced   AndolaSoft

113 connections, 0 recommendations, 374 honor points.
Joined APSense since, March 10th, 2011, From San Jose, United States.

Created on Feb 10th 2022 01:23. Viewed 173 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.