0

I go through all the elements of the array and form a request for each, how can I display a success message only after all requests have been completed?

  saveAllPages() {
    const allStagesData = JSON.parse(JSON.stringify(this.data.pages)).map(el => {
        const id = el.id
        apiService.postCurrentScript(el, id)

   
      ??? alert('success')
}

Maybe I should use Promise all, but how implement it here?

alex
  • 27
  • 5

1 Answers1

1

I would suggest using async-await as it makes code cleaner. Async-await is just an alternaitive to using promises and is many times considered a better choice. I have converted your function into arrow function. The implementation is given below:

saveAllPages = async () => {
    const allStagesData = await (function which returns promise);
    if(allStagesData){
        alert("success");
    }    
}

saveAllPages();
noob_nerd
  • 531
  • 1
  • 6
  • 21
  • 1
    I wonder how is it working? await is working only on Promises (such as function which return Promise, or `Promise.all` etc.). `map` doesn't return Promise so `await` has no effect.. – Mosh Feu Nov 26 '20 at 14:31
  • You're right @MoshFeu . I was focusing on the usage of async/await. So, i will edit the code! – noob_nerd Nov 26 '20 at 17:26