How can I encode/convert multiple video files one by one using fluent ffmpeg?

I have a nodejs script which encodes/converts multiple video files using the fluent-ffmpeg package. Since FFmpeg is very resource intensive my Linux server runs out of resources and my scripts start throwing an error. This is because all the videos are encoded concurrently.
Is there a way to encode the videos one at a time and not concurrently?
My code snippet looks like this:
const Ffmpeg = require(“fluent-ffmpeg”);
videos.forEach(video=> {
covertVideo(filename, (compressionResult)=>{
//do something here
function covertVideo(filename, callback){
var ffmpeg = new FfmpegCommand(filename);
ffmpeg.addOptions(“-threads 1”)
.on(‘error’, function(err) {
console.log('An error occurred: ’ + err.message);
return callback(false);
.on(‘end’, function() {
console.log(‘Compression finished !’);
return callback(true);

You will have to make the function convertVideo return a promise which gets resolved by ffmegs end callback. This would allow the iterator to await the response from the convertVideo function which in turn would process the videos one at a time and thus manage the resource usage.

Great! works for me, Thanks.