Home > front end >  The problem of synchronization request
The problem of synchronization request

Time:12-10

Using the vue development, meet the problem of synchronous request, I need to traverse perform an upload images of operation, for all images uploaded to perform data submitted, code logic about the following
 
Methods: {
Submit () {
Enclosing uploadImg ()
//waiting for the picture to finish and then perform the following actions
.
},

UploadImg () {
Enclosing imgArray. ForEach (item=& gt; {
DoUpload (item)/upload/execution, it is a network of the asynchronous request
})
}
}


I tried to use the following way to synchronize
 
Methods: {
Submit () {
Enclosing uploadImg ()
//waiting for the picture to finish and then perform the following actions
.
},

Async uploadImg () {
Await this. ImgArray. ForEach (item=& gt; {
DoUpload (item)/upload/execution, it is a network of the asynchronous request
})
}
}


 
Methods: {
Async submit () {
Await this. UploadImg ()
//waiting for the picture to finish and then perform the following actions
.
},

UploadImg () {
The return of new Promise ((resolve, reject)=& gt; {
Enclosing imgArray. ForEach (item=& gt; {
DoUpload (item)/upload/execution, it is a network of the asynchronous request
})
The resolve ()
})
}
}

Can achieve synchronization effect, picture upload and other operation is executed asynchronously, excuse me each front-end bosses this need to do

CodePudding user response:

You put other operating upload network request to return the result of implement good

CodePudding user response:

Define a variable to change the value of the variable to monitor the variable do after upload other actions

CodePudding user response:

 


Methods: {
Async submit () {
Await this. UploadImg ()
//waiting for the picture to finish and then perform the following actions
.
},

UploadImg () {
Const arr=[]
Enclosing imgArray. ForEach (async (item)=& gt; {
Arr. Push (doUpload (item))/upload/execution, it is a network of the asynchronous request
})
Return Promise. All (arr)
}
}

//try this


CodePudding user response:

Line 10 no async
  • Related