简体   繁体   English

Listr:在阵列图上可观察到

[英]Listr: Observable on array map

I want to download a group o files and reporting the progress using Listr and RxJs but when i run it loops and display incorrect index of the array. 我想下载一个o组文件并使用ListrRxJs报告进度,但是当我运行它时会循环并显示错误的数组索引。

That's the code of the task [source code] : 这就是任务的代码[源代码]

{
        title: 'Downloading...',
        task: (ctx) => rx.Observable.create((observer) => {
            const count = ctx.photos.length;
            const urls = ctx.photos.map((photo) => photo.urls.raw)
            urls.forEach(async (url, index) => {
                let data = await download(url);
                if ( index !== count - 1 ) {
                    observer.next(index);               
                } else {
                    observer.complete()
                }
            })
        })
}

That's the result (is not the exact same function, but the problem is the same) 这就是结果(功能不完全相同,但问题是相同的)

结果

.forEach will run synchronously even though the callback to it is using async / await . 即使.forEach的回调使用async / await ,它也将同步运行。 To be honest I think this is a better approach since it allows multiple downloads to happen at once and you don't have to depend on the order of the downloads. 老实说,我认为这是一种更好的方法,因为它允许一次进行多次下载,而您不必依赖下载的顺序。 If you want this to display in order you can just maintain your own counter: 如果您希望以此顺序显示,则只需维护自己的计数器即可:

let downloadedCount = 0;
urls.forEach(async (url, index) => {
  let data = await download(url);
  if ( index !== count - 1 ) {
    observer.next(++downloadedCount);               
  } else {
    observer.complete()
  }
})

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

相关问题 如何将可观察到的数组形式映射到另一个数组 - How to map array form observable to another array 将各种json数据映射到可观察数组 - map varying json data to observable array 如何将新的对象数组映射为不可观察? - How to map new array of objects out of observable? 如何 map 到一个 Observable 数组中的多个对象? - How to map to multiple objects within one Observable array? Rxjs:map 数组字段中的每个元素来自 object 的可观察对象和另一个可观察对象 - Rxjs: map each element in an array field from an observable of object with another observable 可观察数组的淘汰赛可观察数组 - Knockout Observable Array of Observable Array 将可观察数组的可观察值转换为可观察数组的值 - Convert observable of array of observable into observable of array 可观察的 map 属性基于另一个可观察的 - Observable map property based on another observable Angular(Javascript)如何使用Observable数组的map和filter过滤datetime并返回新相似对象的数组 - Angular (Javascript) how to use map and filter for an Observable array to filter for datetime and return array of new similar objects 尝试将JSON数组中的对象转换/映射到KO可观察函数 - trying to convert/map the Objects in my JSON array to my KO observable function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM