首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用数组映射async/await更新对象

如何使用数组映射async/await更新对象
EN

Stack Overflow用户
提问于 2020-06-07 19:34:44
回答 2查看 312关注 0票数 0

我希望我的方法生成一个表示目录树的js对象,我使用directory-tree:https://www.npmjs.com/package/directory-tree。而且它是有效的。

我的问题是,当我想要计算视频文件(MP4)的持续时间时,我想给每个项目添加一个属性" duration“,但id不起作用,因为它返回一个promise。

为了计算持续时间,我使用get-video-duration:https://www.npmjs.com/package/get-video-duration

以下是代码

代码语言:javascript
复制
const {getVideoDurationInSeconds} = require('get-video-duration');
const dirTree = require("directory-tree");



async function getVideoDuration(path) {
    const duration = await getVideoDurationInSeconds(path);
    return duration;
}

const anAsyncMethod = async (item, PATH, stats) => {
    if (item.type === 'file' && item.extension === '.mp4') {
        const duration = await getVideoDuration(item.path);
        item.duration = duration;
        console.log(item); // it works here
        return item;
    }

}
const getCourseVideos = async (path) => {
    const tree = await dirTree(path, { extensions: /\.mp4/,} , anAsyncMethod);
    return (tree);
};

getCourseVideos('/PATH/TO/YOUR/FOLDER/OF/VIDEOS').then((tree)=> {
    console.log(tree);
});

实际输出的示例:

代码语言:javascript
复制
{
  "path": "/MY/PATH/HERE",
  "name": "react",
  "children": [
    {
      "path": "/MY/PATH/HERE/lesson",
      "name": "lesson",
      "children": [
        {
          "path": "/MY/PATH/HERE/lesson/lesson10.mp4",
          "name": "lesson10.mp4",
          "size": 38642184,
          "extension": ".mp4",
          "type": "file"
        },
        {
          "path": "/MY/PATH/HERE/lesson/lesson11.mp4",
          "name": "lesson11.mp4",
          "size": 41421609,
          "extension": ".mp4",
          "type": "file"
        }
    }
  ],
  "size": 17042089152,
  "type": "directory"
}
...
EN

回答 2

Stack Overflow用户

发布于 2020-06-07 19:50:12

您可以在promises字符串中添加一个中间操作,然后在树中查找视频。是这样的:

代码语言:javascript
复制
getCourseVideos('/PATH/TO/YOUR/FOLDER/OF/VIDEOS')
  .then(async (tree)=> {
    const queue = [tree];
    while (queue.length > 0) {
      const item = queue.shift();
      if (item.children) {
        for (let it of item.children) {
          queue.push(it)
        }
      } else if (item.type === 'file') {
        item = await anAsyncMethod(item.path);
      }
    }
    return tree;
  })
  .then(tree => console.log(tree));
票数 0
EN

Stack Overflow用户

发布于 2020-06-07 20:08:11

我找到了一个解决方案:下面是新的代码

代码语言:javascript
复制
const {getVideoDurationInSeconds} = require('get-video-duration');
const dirTree = require("directory-tree");


async function getVideoDuration(path) {
    const duration = await getVideoDurationInSeconds(path);
    return duration;
}

const recursiveAsync = async (item, PATH, stats) => {
    if (item.type === 'directory') {
        item.children = await Promise.all(item.children.map(async (item) => {
            return recursiveAsync(item);
        }));
    }
    if (item.type === 'file' && item.extension === '.mp4') {
        const duration = await getVideoDuration(item.path);
        item.duration = duration;
    }
    return item;
}
const getCourseVideos = async (path) => {
    const tree = dirTree(path, {extensions: /\.mp4/,});

    await Promise.all(tree.children.map(async (item) => {
        return recursiveAsync(item);
    }));
    return tree;
};

getCourseVideos('/PATH/TO/YOUR/FOLDER/OF/VIDEOS').then((tree) => {
    console.log(tree);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62244686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档