Promises
With Promises, we can organize the sequence of our code in a slightly easier-to-maintain way. A Promise is a special object that connects code that needs to produce a result and the code that needs to use this result in the next step.
When we create a Promise, we give it a function. In the following example, we use a convention that we have seen a lot; we are creating a function on the spot. So, inside the argument list we are defining the function, often done using arrow functions as well. This function needs two parameters, and these parameters are callbacks. We have called them resolve
and reject
here.
You can call these parameters anything you want, but resolve
or res
and reject
or rej
are most common.
When resolve()
is called, the Promise is presumed to be successful and whatever is between the arrows is returned and used as input for the then
method on the Promise
object. If reject()
is called, the Promise
failed and the catch()
method on the...