How do I make sequential requests in JS (VueJS)?
-
There is an array of objects, the number of objects differs depending on the selected parameters, most likely it can be 16 objects, of which from 1 to 16 can be selected.
Array example:
let arrayObjects = [ { id: 1, name: 'test1', selected: true, }, { id: 2, name: 'test2', selected: false, }, { id: 3, name: 'test3', selected: true, }, ]
You need to make a request to the server for each object with selected = true
The request must be sequential, first to test1, after the server responds to test3
How can this be done?JavaScript Anonymous, Mar 5, 2020 -
const quiz = async (arrayObjects) => {
for (const object of arrayObjects) {
if (object.selected) {
const result = await fetch(...);
}
}
}Anonymous -
Weird question. They usually ask how to do them at the same time.
https://codepen.io/deliro/pen/GRqWQYW?editors=1010Anonymous -
You can use async / await or promises on requests.
https://learn.javascript.com/async-await
https://learn.javascript.com/promiseAnonymous
3 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!