Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Promises

So what are promises

Promises is just another syntactic sugar that was invented in order to avoid the pyramid of doom from happening. 

Promises are objects and any time we have an asynchronous operation, we can create a new promise object like we did on line 7 by using the new constructor for objects.

Now as we know, objects have some properties and promises are no exception to that. Promises have a state and a value. And if you run the code above you can see both of these properties to your console and the promise object itself (because we console the promise on line 12).

Until the asynchronous operation has finished and we have the data back, the browser can do other things. That means, that in the meantime the state of the promise is PENDING and the value undefined

Once the operation is finished the  state changes either to RESOLVED or REJECTED (in case of an error, data was not there etc) and the value corresponds the data that came back. 

Attention! In order for our function to be an asynchronous function it is always mandatory that we don't return a value, but the promise itself. The value is returned indirectly by passing it as an argument into the resolve function as seen above.