I'm building an app where I need to get metadata from files and I do that with "music-metadata" from npm but I need to get it for an array of files. The module returns promises, so I need to await them / use .then()
but when I use await it doesn't await it, it just returns an array of empty objects and when I use .then()
it returns one object with metadata not the array. I've only gotten it to work with a for/in
loop:
const getMetadata = async (dirPath, fileName) => {
return new Promise((resolve, reject) => {
if (typeof dirPath !== "string") {
reject(new Error("directory path must be given as String!"));
}
if (typeof fileName !== "string") {
reject(new Error("file name must be given as String!"));
}
mm
.parseFile(`${dirPath}/${fileName}`)
.then(metadata =>
resolve({ directory: dirPath, fileName: fileName, ...metadata })
)
.catch(err => reject(err));
});
};
let musicMetadata = [];
for (const file in musicFiles) {
try {
musicMetadata.push(await getMetadata(directoryPath, musicFiles[file]));
} catch (err) {
console.error(err);
}
}
But is there any way of doing it more "functional programming" like, similar to this:
const getMetadata = (dirPath: string, fileName: string): Promise => {
return new Promise((resolve, reject) => {
mm.parseFile(`${dirPath}/${fileName}`)
.then(metadata =>
resolve({ directory: dirPath, file: fileName, ...metadata })
)
.catch(err => reject(err));
});
};
const musicMetadata = await musicFiles.map(async (file: string) => {
return new Promise((resolve, reject) => {
getMetadata(directoryPath, file)
.then(metadata => resolve(metadata))
.catch(err => reject(err));
});
});
TIA for any replies!