Search code examples
node.jspromisebluebird

promise flow - bluebird


I'm trying to use ffmpeg to cut a few seconds of a directory with mp3s. But my actual problems comes with using promises.

Instead of starting one ffmpeg process after the other it starts up one for each file immediately. My guess is the promise isn't waiting for the resolve and I didn't understand it properly.

var P = require('bluebird');
var fs = P.promisifyAll(require("fs"));

function transcode(filename) {
    return P.delay(1000).then(function() {
        console.log(filename);
    });
}
var in_dir = "./somedir/";

var getFiles = function(){
    return fs.readdirAsync(in_dir);
};

getFiles().mapSeries(function(filename){
    transcode(filename);
});

Solution

  • I've created a simplified version of your code. The only missing thing was the return statement for the final closure:

    var P = require('bluebird');
    var fs = P.promisifyAll(require("fs"));
    
    function transcode(filename) {
        return P.delay(1000).then(function() {
            console.log(filename);
        });
    }
    var in_dir = "./somedir/";
    
    var getFiles = function(){
        return fs.readdirAsync(in_dir);
    };
    
    getFiles().mapSeries(function(filename){
        return transcode(filename);
    });