asyncawait icon indicating copy to clipboard operation
asyncawait copied to clipboard

Make Bluebird countFiles example idiomatic

Open joepie91 opened this issue 9 years ago • 1 comments

Given that this is supposed to be a comparison of code styles, it's rather important that the examples reflect the idiomatic/optimal style for a given approach. This wasn't the case with the original Bluebird example, which used virtually no Bluebird utility methods and contained superfluous code.

joepie91 avatar May 18 '16 11:05 joepie91

Hi @joepie91,

Looks good, thanks! Before committing this, would you mind replacing the arrow functions with ordinary function expressions? None of the code examples have been upgraded to use ES5 features yet, and I'd like to do that consistently across all of them as a separate PR (if you feel like doing this, be my guest!)

Once we are using ES5, I'd probably shorten your code even more as:

module.exports = function countFiles(dir, cb) {
    return Promise
        .try(() => fs.readdirAsync(dir))
        .map(file => path.join(dir, file))
        .map(file => fs.statAsync(file))
        .filter(stat => stat.isFile())
        .then(stats => stats.length)
        .nodeify(cb);
}

yortus avatar Jun 07 '16 05:06 yortus