January 2018
Beginner
658 pages
13h 10m
English
Promise chaining is the idea of having multiple promises run in a sequence. For example, I want to take an address and convert that into coordinates, and take those coordinates and convert them into weather information; this is an example of needing to synchronize two things. Also, we can do that really easily using promise chaining.
In order to chain our promises, inside our success call we'll return a new promise. In our example, we can return a new promise by calling asyncAdd again. I'll call asyncAdd next to the res and console.log statements, passing in two arguments: the result, whatever the previous promise has returned, and some sort of new number; let's use 33:
asyncAdd(5, 7).then((res) => { console.log('Result:', ...