Promises Chaining
MediumIn plain terms
Chain .then() calls - each returns a value or Promise for the next. .catch() catches any error in the chain. Avoid nesting; flatten with returns.
What you need to know
- •then returns Promise
- •catch handles errors
- •Flatten chains
Try it yourself
Copy the code below and run it in your browser console or a code editor:
fetch('/api/1').then(r=>r.json()).then(d=>fetch('/api/2?q='+d.id)).then(r=>r.json()).then(console.log).catch(console.error);Learn more
Dive deeper with these trusted resources: