I am trying to code multiple dependent fetch get requests. Here number fetch call is a variable and each new request has previous response dependency
i try below pseudocode code, just need to set finalResult var after last fetch call
var finalResult = null;
af = async () => {
Promise.resolve()
.then(x => console.log("first then"))
.then(x_FromPreviousResponse => console.log("second then"))
.
.
.
.then(z_FromPreviousResponse => finalResult = z_FromPreviousResponse)
Promise.resolve()
}
af()
kindly give me code example or pseudocode
2
Answers
You can use the
then
method as shown in the example below to make a new fetch call based on the results of the previous call after the previous call has completed. See the example code below.To make multiple dependent fetch requests, using the
async/await
syntax, makes it easier(to read of course since it’s a syntactic sugar) to handle asynchronous code.Below is the small working code of aforementioned pseudocode, which uses JSON placeholder API. The example: