For example, I have a function : async updateOne(track) that get audio file of each track and save it on a repertory after making some changes to the audio.
This is the code that got all the tracks and loop on it and use the updateOne function on each track :
exports.updateAllTracksWithNewTag = async (req, res) => {
try {
const allTracks = await Tracks.findAll()
await Promise.all(
allTracks.map(async (track) => {
await this.updateOne(track)
})
)
return res.status(200).json({ message: 'All tracks changes done' })
} catch (error) {
return ResponseErrorFormated.responseError(error, res)
}
}
updateOne(track) function :
exports.updateOne= async (track) => {
const filesToDeleteFromTemp = []
try {
const fileAudioURL = track.MP3_audio_url
const originalFileReadstream = await AWSS3Utils.getReadStream(
URLConfig.URL_ASSETS.PRIVATE.USERS_TRACKS_AUDIOS.path.slice(1) + fileAudioURL
)
const tempPathToSaveStreamGetted = 'assets/temp/audios/'
// Saving the file to the temp repertory
await FilesUtils.saveReadStreamAsFile(originalFileReadstream, tempPathToSaveStreamGetted, fileAudioURL)
filesToDeleteFromTemp.push(tempPathToSaveStreamGetted + fileAudioURL)
const fileInfosForTag = {
path: tempPathToSaveStreamGetted + fileAudioURL,
filename: fileAudioURL,
}
// Make process to the audio track that take 5sec minimum
const resultTaggedMP3 = await FilesFormater.createTaggedAudioFromMP3(fileInfosForTag)
const readStreamTaggedMP3 = resultTaggedMP3.readStream
const finalFilePathTaggedMP3 = resultTaggedMP3.finalFilePath
const finalFileNameTaggedMP3 = resultTaggedMP3.finalFileNameTaggedMP3
const newFileKeyMP3 =
URLConfig.URL_ASSETS.PUBLIC.USERS_TRACKS_TAGGED.path.slice(1) + finalFileNameTaggedMP3
filesToDeleteFromTemp.push(finalFilePathTaggedMP3)
// ReUpload the new file changed
await AWSS3Utils.uploadFileFromReadstream(readStreamTaggedMP3, newFileKeyMP3)
// Delete files from temp repertory
await FilesUtils.unlinkFiles(filesToDeleteFromTemp)
} catch (error) {
await FilesUtils.unlinkFiles(filesToDeleteFromTemp)
throw error
}
}
It should save the file of the first track, then wait 5sec or more for process, then reupload the file and delete it, and after do the same with the next track.
But when I execute it, he save all the files to the repertory and just after he make the process of each track and reupload it and delete it.
How can I prevent it to save all the track files in one time ? Because it take a lot of powerful when he do this quickly.


