Use of Async AWAIT: Multiple values ​​are submitted in order

tags: vue  js  

business

Click the person data submitted by the submit button loop to the submission API separately
Since the submission method is Ajax is asynchronous, the simple loop submission will appear in order
So use async AWAIT to block

Code

    onSubmit() {
      let that =this
      Let asynclist = [] // Ajax method array
      for(let i=0;i<that.selectEmployees.length;i++) {
                 Let Arg = That.SelectemPloyees [i] // Need to pass the parameters
        async function test(arg){enroll(arg.employeeId, that.courseId, 'UNCANCELED')} 
                 Asynclist.push ({func: test, argument: arg}) // Put the test method in an array, by the way
      };
      async function async(){
                 For (var i = 0; i <askNCLIST.LENGTH; i ++) {// cycle AJAX method array
                     AWAIT AsyncList [i] .func (asynclist [i] .Argument) .Then (() => {//. THEN can further process the return value
                         Console.log ('has executed' + i)
          })
        }
      }
             Async (). Then (() => {// All Ajax execution processing
                 Console.log ('execution is ")
      })
    },

Small Demo, execution order

The above code can actually simplify

function fuc1(){
      return new Promise((resolve)=>{
      setTimeout(() => {
          resolve('fuc1')
          console.log('fuc1')
       }, 2000);
      })
       
    };
function fuc2(){
      return new Promise((resolve)=>{
        setTimeout(() => {
          resolve('fuc2')
          console.log('fuc2')
       }, 1000);
      })
       
    };
var list=[fuc1,fuc2]
async function fuc(){
    for(var i=0;i<list.length;i++){
        console.log(list[i])
        await  list[i]().then(console.log(i+'done'))
        }
    };
fuc().then(()=>{console.log('all done')})

AWAIT will block the current async

The problem with the execution order can be referred to, quite quite detailed
https://blog.csdn.net/m0_37922914/article/details/108564884

Intelligent Recommendation

Use async / await asynchronous processing multiple images simultaneously uploaded to the server

Encountered such a demand on the project, is the question of feedback, allowing the user to upload pictures. Because it is a multi-picture simultaneous uploads, and taking into account the user might ...

Promise, async, await use

1.Promise The understanding of Promise is an object, just an object that can be executed asynchronously. Promise objects have the following three states 1. resolve 2.reject 3.unresolve: Initialization...

Use of async/await and precautions

Using async / await, with a promise, you can handle asynchronous processes by writing code that looks like synchronization, improving the simplicity and readability of the code. This article describes...

Async and await use

Async and await use Q: Suppose there are 3 interfaces, A, B, and C are dependent on each other. A–>B–>C Simulation code API promise: generator async effect Solve deep nesting Charact...

More Recommendation

Es6 async await use

Asynchronous function is used. During the execution of the function, it waits for an asynchronous function to execute before executing the next content. Usually the current function needs to use the v...

Es6 async+await use

[1] Here mainly look at the role of the await command: When the function is executed, once it encounters await, it will return first, wait until the end of the asynchronous operation, and then execute...

How to use async and await

How to use async and await Promise is still the original usage. The use of async and await must be in a function block. Both need to be used together....

Vue--- in the use of async and await

Many times I don't have a lot of business, I need to call multiple background interfaces in turn, and the latter interface needs to rely on the response result of the previous interface. If the previo...

Es6 async and the use of await

Fs read file es5 Es6 writing 1 Es6 writing 2...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top