menu
Guide To Use JavaScript Promises And Fetch API
Guide To Use JavaScript Promises And Fetch API
JavaScript is an interpreted language where codes are executed line by line. Promises are JavaScript objects that represent an eventual completion.

How To Use JavaScript Promises And Fetch API

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.

 

Read More On JavaScript Promises and Fetch API