4 ms·
1. Makes an HTTP call, waits for it to complete, and prints the result; 2. *Then* makes other two parallel HTTP calls; 3. Wait for both to complete, *then*
by tejohnso 9y ago
1. Makes an HTTP call, waits for it to complete, and prints the result;
2. *Then* makes other two parallel HTTP calls;
3. Wait for both to complete, *then* print their result.
"For such a simple example, we ended up with 2 nested then callbacks ..."
I've slightly rewritten the third point above to state my point.
Which is that I appreciate how promises reflect the actual situation. When describing the requirement, you'll use the word 'then' twice, and the code with promises ends up having two 'then's in it. It might be somewhat trivial, but I like it. It seems fitting to me that I describe "do this then that" and the code will be `doThis().then(that)`
One thing about the promise example that's somewhat unfair to the promise side, there is a double nesting of the callbacks. That's not necessary.
rp('http://example.com/')
.then((resp)=>{
console.log(resp)
return Promise.all([rp('http://example.com/'), rp('http://example.com/')])
})
.then(resp=>{
console.log(resp[0])
console.log(resp[1])
})