javascript - Parallel asynchronous iteraor - is it possible?
问题描述
Right now I have the following code:
import axios from 'axios'
const urls = ['https://google.com', 'https://yahoo.com']
async function* requests() {
for (const url of urls) {
yield axios.get(url)
}
}
;(async () => {
for await (const n of requests()) {
console.log(n.config.url) // prints https://google.com and then https://yahoo.com
}
})()
As is, the requests won't block the single thread of node, but they will happen in sequence. I'm wondering if it would be possible to change the code to force parallelism.
解决方案
“更简单”的 no-deps 方法是对它们进行批处理并使用Promise.all
import axios from 'axios'
const urls = [
'https://jsonplaceholder.typicode.com/todos/1',
'https://jsonplaceholder.typicode.com/posts/1',
'https://jsonplaceholder.typicode.com/users/1',
'https://jsonplaceholder.typicode.com/comments/1'
]
async function* requests(batchSize = 1) {
let batchedRequests = [];
for (const url of urls) {
batchedRequests.push(axios.get(url));
if (batchedRequests.length === batchSize) {
yield Promise.all(batchedRequests);
batchedRequests = [];
}
}
if (batchedRequests.length) { //if there are requests left in batch
yield Promise.all(batchedRequests);
}
}
;(async () => {
for await (const batch of requests(2)) {
batch.forEach(n => console.log(n.config.url)) // prints https://google.com and then https://yahoo.com
}
})()
您可以使用rxjs
observables 在灵活性方面获得类似的结果,但它是另一个库,如果您不熟悉反应流,它可能会更复杂。这是我在该主题上找到的详细帖子:https ://medium.com/@ravishivt/batch-processing-with-rxjs-6408b0761f39
推荐阅读
- javascript - 如何正确解决此承诺以获取图像数据 url?
- c - 输入字符串后C程序没有结束?
- amazon-web-services - 为什么 Cognito 注册显示 ??phone_number/email_en_US?? 作为用户名输入?
- javascript - 你如何从 json 渲染对象
- css - 使用填充将覆盖添加到 div
- vue.js - 如何在 Webpack 中启用源映射?
- google-cloud-run - 如何允许从 Google Cloud Run 到外部 MySql 实例的出站请求
- php - RSS 阅读器适用于 LocalHost 但不适用于共享主机
- c# - 尝试通过 mciSendString (MCIERR_CANNOT_LOAD_DRIVER) 播放 mp3 文件时出现问题
- r - 澄清函数内的列表子集