Starting a Promise chain with Promise.resolve
In this chapter's preceding recipes, we've been creating new promise
objects with the constructor. This gets the jobs done, but it creates a problem. The first callback in the promise chain has a different shape than the subsequent callbacks.
In the first callback, the arguments are the resolve
and reject
functions that trigger the subsequent then
or catch
callbacks. In subsequent callbacks, the returned value is propagated down the chain, and thrown errors are captured by catch
callbacks. This difference adds mental overhead. It would be nice to have all of the functions in the chain behave in the same way.
In this recipe, we'll see how to use Promise.resolve
to start a promise chain.
Getting ready
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create...