General: Promises A-sync Javascript

Photo by Palo Cech from Pexels

Asynchronous javascript is an important part of every website's code nowadays, it allows for multiple other site-wide functions to play while waiting for a function to run that might take too much time or stall the end-user experience.

Promises are the modern-day way of handling async javascript. An example of a promise would be when you’re fetching data from the back end to the front end. when you use fetch() you’re sending a request and awaiting a promise. The promise is what will contain the data that you are waiting for, We then use .then() to let us manipulate the data after we’ve got it.

If you were to couple a javascript promise with await, you would be allowed to suspend the current javascript function while you “await” the return of the promise. once the promise has been fulfilled, either by returning fulfilled or rejected, you can choose what to do at that point.

You can also chain multiple promises together in order to manipulate larger arrays of data.