Reputation: 33
Do these essentially do the same thing? I came across this while in a Promise example and the lecturer had returned the fetch but wouldn't example 1 be another viable way of doing this as well?
1
fetch('url1')
.then(data => data.json())
.then(() => {
fetch('url2')
.then((response) => {
// do things
})
})
2
fetch('url1')
.then(data => data.json())
.then(() => {
return fetch('url2')
})
.then((response) => {
// do the same thing but at same scope
});
Upvotes: 0
Views: 25
Reputation: 943214
In example 1, the second then()
resolves to undefined
In example 2, the second then()
adopts the promise returned by the second fetch
and so the final then
could return something usable at the top level return value.
Either way. It's quite verbose code that could be a lot clearer using async
and await
.
Upvotes: 1