According to the MDN Web Docs “ A promise is a proxy for a value not necessarily known “. In other words, a promise is an intermediary between the web application and the server.
How does a Promise work?
fetch('https://jsonplaceholder.typicode.com/todos/1') .then( console.log(response.json()))
Then the second return is effectively the server response
fetch('https://jsonplaceholder.typicode.com/todos/1') .then( console.log(response.json())) .then( console.log(json))
1- Simplified Error Management: Promises allow you to catch errors in an easier and more organized way compared to nested callbacks. Instead of dealing with multiple levels of nested callbacks, Promises can use a single function
catch() to handle all errors in one place.
2- More readability and less nesting: Using Promises can improve code readability as Promises allow code to be written in a more logical and linear sequence rather than using nested callbacks which can make code difficult to read . This can make the code easier to understand and maintain.
In other articles we will talk more about synchronous functions with async/await.