简体   繁体   English

多个 jQuery 使用 async/await 模式并行获取

[英]Multiple jQuery gets in parrallel using the async/await pattern

I want to access three api methods in parallel using jQuery and the async/await pattern in Javascript.我想使用 jQuery 和 Javascript 中的 async/await 模式并行访问三个 api 方法。 The results of the three calls should be written into a Knockout Observable.三个调用的结果应该被写入一个 Knockout Observable。 I created the following code:我创建了以下代码:

await Promise.all([
    (async() => vm.allHaulers(await $.get(baseAddress + 'GetCompleteHaulerList')))(),
    (async() => vm.allPlants(await $.get(baseAddress + 'GetCompletePlantList')))(),
    (async() => vm.allTrailers(await $.get(baseAddress + 'GetCompleteTrailerList')))()
]);

This looks very bulky with all the brackets.所有的括号看起来都非常笨重。 Is there a more elegant solution to this?有没有更优雅的解决方案?

Just use .then of the Promise returned by $.get只需使用 $.get 返回的 Promise 的 .then

await Promise.all([
    $.get(baseAddress + 'GetCompleteHaulerList').then(vm.allHaulers),
    $.get(baseAddress + 'GetCompletePlantList').then(vm.allPlants),
    $.get(baseAddress + 'GetCompleteTrailerList').then(vm.allTrailers)
]);

Sometimes the "old" tools work better :p有时“旧”工具效果更好:p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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