I'm trying to understand why the below promise setups don't work.
(Note: I already solved this issue with async.map. But I would like to learn why my attempts below didn't work.)
The correct behavior should be: bFunc should run as many time as necessary to fs read all the image files (bFunc below runs twice) and then cFunc console prints "End".
Thanks!
Attempt 1: It runs and stops at cFunc().
var fs = require('fs');
bFunc(0)
.then(function(){ cFunc() }) //cFunc() doesn't run
function bFunc(i){
    return new Promise(function(resolve,reject){
        var imgPath = __dirname + "/image1" + i + ".png";
        fs.readFile(imgPath, function(err, imagebuffer){
            if (err) throw err;
            console.log(i)
            if (i<1) {
                i++;
                return bFunc(i);
            } else {
                resolve();
            };
        });
    })
}
function cFunc(){
    console.log("End");
}
Attempt 2: In this case, I used a for-loop but it executes out of order. Console prints: End, bFunc done, bFunc done
var fs = require('fs');
bFunc()
        .then(function(){ cFunc() })
function bFunc(){
    return new Promise(function(resolve,reject){
        function read(filepath) {
            fs.readFile(filepath, function(err, imagebuffer){
                if (err) throw err;
                console.log("bFunc done")
            });
        }
        for (var i=0; i<2; i++){
            var imgPath = __dirname + "/image1" + i + ".png";
            read(imgPath);
        };
        resolve()
    });
}
function cFunc(){
    console.log("End");
}
Thanks for the help in advance!
 
     
     
     
     
     
     
    