Answer the question
In order to leave comments, you need to log in
Why doesn't async.each make execution synchronous?
With the following code, the result check function is executed first:
async.forEachOf(audios, function(audio, key, callback) {
Audio.getMetadata(audio.path, function(err, metadata) { // асинхронная
if (err) {
console.log(err);
return callback(err);
}
fullAudios.push(Audio.setMetadata(audio, metadata)); // return audio with metadata
callback();
});
}, function done(err) {
if (err) {
console.log(err);
}
console.log(fullAudios);
});
Answer the question
In order to leave comments, you need to log in
And so, here is my solution, not the fact that the code is correct, but it works.
module.exports.getMetadates = async function getMetadates(audios) {
return await Promise.all(audios.filter((audio) => fs.existsSync(audio.path)).map(async function(audio) {
let stream = fs.createReadStream(audio.path);
let metadata = await getMetadataSync(stream);
return Audio.setMetadata(audio, metadata);
}));
}
async function getMetadataSync(file) {
return await new Promise ((resolve, reject) => {
musicMetadataReader(file, (err, metadata) => {
if (err) {
return reject(err);
}
if (metadata.picture.length > 0) {
metadata.picture[0].base64String = getDecodedPicture(metadata.picture[0]);
}
resolve(metadata);
});
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question