Web Technologies

Basics of promise in fetch api to make ajax call – Tutorial 8

Let’s get rid of XMLHttpRequest and its methods like open, send, etc. We now use fetch API to make ajax calls.

Fetch uses Promises

This means data from a fetch request is therefore given back to us, via a promise

You can’t get the value from a promise directly  

DON’T PANIC. To get the value from a promise, all you need to do is register a .then() statement. This is a callback function that JavaScript will execute when the Promise has either been successful or failed

The .then() statement takes 2 optional parameters – the onFullfilled() method and the onRejected() method

Market convention is to use only the onFullfilled() method, and to catch all other errors in a universal .catch() block

Next lession