M
M
Mikhail Smirnov2020-10-26 17:20:02
JavaScript
Mikhail Smirnov, 2020-10-26 17:20:02

How to execute sequential requests in JS (VueJS)?

There is an array of objects, the number of objects varies depending on the selected parameters, most likely it can be 16 objects, of which 1 to 16 can be selected

Example of an array:

let arrayObjects = [
  {
    id: 1,
    name: 'test1',
    selected: true,
  },
  {
    id: 2,
    name: 'test2',
    selected: false,
  },
  {
    id: 3,
    name: 'test3',
    selected: true,
  },
]


It is necessary to make a request to the server for each object with selected = true The

request must be sequential, first to test1, after the server response to test3

How can this be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-10-26
@fortoster82

const quiz = async (arrayObjects) => {
  for (const object of arrayObjects) {
    if (object.selected) {
      const result = await fetch(...);
    }
  }
}

A
Alexander Veksler, 2020-10-26
@alexvexone

You can use async/await or promises for requests.
https://learn.javascript.ru/async-await
https://learn.javascript.ru/promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question