简体   繁体   中英

how to update objects with array map async/await

I want my method to generate a js object representing a directory tree, I use directory-tree: https://www.npmjs.com/package/directory-tree . and it works.

My question is when i want to calculate the duration of video files (MP4) I would like to add an attribute "duration" to every item but id doesn't work, because it returns a promise.

to calculate duration i use get-video-duration: https://www.npmjs.com/package/get-video-duration

Here is the code

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

an example of the actual output:

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

You can add a middle action in the promises string and then go thrown the tree looking for the videos. Is something like this:

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

i kinda found a solution: here is the new code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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