如何使用数组映射异步/等待更新对象

时间:2020-06-07 11:34:44

标签: javascript node.js promise async-await

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

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

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

这是代码

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);
});

实际输出示例:

{
  "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"
}
...

2 个答案:

答案 0 :(得分:0)

您可以在promise字符串中添加一个中间动作,然后扔到树上寻找视频。是这样的:

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));

答案 1 :(得分:0)

我有点找到解决方法: 这是新代码

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);
});