简体   繁体   中英

When using Promise.All for multiple ajax requests, how do I assign that data to a local variable?

I am trying to use Promise.All() three times. Each one for a specific array of ajax requests that call a function in my code-behind that returns a jsonresult. Is there a way I can get the array return from promise.all and assign it to local array. I originally tried using async:false on ajax but it is incredibly slow. Are there any other possibilities?

You're in luck! Promise.all returns an array, with each element in the output corresponding to the elements of the input.

let asyncResults = await Promise.all([
    asyncOp0(),
    asyncOp1(),
    asyncOp2()
])

At this point, asyncResults is a local array.

asyncResults[0] will be the return of asyncOp0

asyncResults[1] will be the return of asyncOp1

asyncResults[2] will be the return of asyncOp2

It sounds like that's exactly what you want.

If it's not returning fast enough for you, the likely culprit is the HTTP endpoint you're hitting. You should study the performance of that endpoint. Alternatively, if the HTTP response is very large, bad performance is just a consequence of that. The remedy might require you to redesign the server API to return a smaller representation of the data you care about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM