Colleagues, please help.
There are several requests to the server.In replacements, the server creates tasks for these requests and gives the task id by which I check the status of the task execution by another request.
When all tasks are completed, you must continue to execute synchronous code. How to wait for all the tasks?

This is how it works for me:
async getTaskStatus(id, resolve) {
            if(! this.queryCounter) {
                this.queryCounter=0;
            }
            if(this.queryCounter>20) {
                return this.errHandler();
            }
            try {
                const {data: {data: {status}}}=await checkTaskStatus(id);
                if(status ==='done') {
                    return resolve();
                } if(status ==='error') {
                    this.$message.error('Error performing the operation, try again later');
                    this.$router.push('/users');
                } else {
                    setTimeout(()=>{
                        this.queryCounter ++;
                        this.getTaskStatus(id, resolve);
                    }, 500);
                }
            } catch(e) {
                console.error(e);
            }
        };

       try {
                        const {id}=this.ruleForm.user_dto;
                        const tasks=[];
                        tasks.push(updateUserData(id));
                        tasks.push(updateATTRData(id));
                       tasks.push(updateOldATTRData(id));

                        const results=await Promise.all(tasks);
                        for(let i=0;i<results.length;i ++) {
                            if(results[i] .data.validation_result.success === true) {
                                let resolve=()=>Promise.resolve();
                                await this.getTaskStatus(results[i] .data.data, resolve);
                            } else {
                                this.errHandler();
                            }
                        }
                        this.setUser(this.userId);
                        this.editAdmin=false;
                    } catch(e) {
                        this.errHandler();
                        console.error(e);
                    }


Accordingly, the first requests are in the loop and the code execution continues.
In the function of obtaining status, there is recursion via SetTimeout, here, in fact, the code ceases to be asynchronous.
How to wait for getting statuses with completed tasks?

1 Answers 1

Wrap everything into an anonymous async, and then two Promise.all.The point is that you have to"insert" your synchronous code into asynchronous"then" via await or Promise - it does not matter.
Something like this should be the construction
(async()=>{
let reqs=[];

async function doTasks() {
return Promise.all(reqs.map((req)=>doReq(req)))
  .then((tasks) => Promise.all(tasks.map((task)=>checkStatus(task))))
  .catch(console.error);
};

await doTasks();

//do some sync code
})();
  • Thanks, I'll try now – Fine Ferret Jul 15 '19 at 10:35
  • it turns out that checkStatus(task) is executed asynchronously once, and=apparently due to setTimeout, asynchrony falls apart.
    The code is executed, but synchronously.
    – Fine Ferret Jul 15 '19 at 11:27
  • Fine Ferret, checkStatus should return promis.SetTimeout also wrap in promis and you can skip it. – Jack100 Jul 15 '19 at 11:29
  • yes, it turned out, did so:
    return new Promise(async(res)=>{
                            setTimeout(async()=>{
                                this.queryCounter ++;
                                res(this.getTaskStatus(id, resolve));
                            }, 500);
                        });


    Tell me this somehow through async await can be issued?
    Well, or in general, somehow more symmetrical?
    to output errors use reject?
    – Fine Ferret Jul 15 '19 at 11:45
  • Thank you very much! – Fine Ferret Jul 15 '19 at 11:46
  • Fine Ferret, I try to avoid async/await in my code because they are syntactic sugar over Promise by nature.Your code uses both, which, according to me, is"dirt".
    To wrap setTimeout and other callbacks:
    return new Promise((resolve, reject)=>{
      setTimeout(function() {
        if(Math.random()>0.5) {
          return reject(new Error('some error');
        }
        resolve(1);
      }, 1000);
    })
    – Jack100 Jul 15 '19 at 12:01
  • async removed) – Fine Ferret Jul 15 '19 at 12:11