10

I use the bluebird magic

var fs = Promise.promisifyAll(require('fs'));

and use

fs.readdirAsync(dest).then(function (val) {
        return val;
}).then(function (file) {

...

My question is for the following code (which is working) should I use the same and how I am talking about the mkdir function.

function createDir(folder) {
    return function (req, res, next) {
        if (typeof require.cache.per === 'undefined') {
            require.cache.per = {};
            require.cache.per.mk = false;
        }
        if (!require.cache.per.mk) {
            fs.mkdir(folder, function (e) {
                if (!!e && e.code !== 'EEXIST') {
                    console.log('Error to create  folder: ' + err);
                }
                require.cache.per.mk = true;
                next();
            });
        } else {
            next();
        }
    };
}

My Question is should I use promise here or not, what is recommended ? The code is working as expected...

  • 1
    You can just use `mkdirAsync`, promisifyAll creates it for you. – Benjamin Gruenbaum Feb 25 '16 at 14:22
  • @BenjaminGruenbaum -Thanks but how should I call it with the catch and the next inside promise ,can you please provide example? –  Feb 25 '16 at 14:27
  • Exactly the same way you did for `readdir` – Benjamin Gruenbaum Feb 25 '16 at 14:43
  • it is recommended to use promise for an async call. so use the same way that used for readdir – Raja Sekar Mar 01 '16 at 12:16
  • That must be your choice. If u want use it - go ahead, if u OK with callbacks - ok :). Btw what the reason using `!!e` :) ? – loadaverage Mar 08 '16 at 00:53
  • @loadaverage `!!` is used to boolean-ify a value (see: http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – souldzin Mar 08 '16 at 03:23
  • @souldzin yep, I know. But if error `(e)` is exists then `!!e` doesn't make any sense coz it anyway `true`, otherwise if there are no error and `e` is `null` - `!!e` means false (just like `null` means) and doesn't affect if..else statement. – loadaverage Mar 08 '16 at 04:05
  • @loadaverage oh yeah, it's pointless in this case, but sometimes it's just fun to use `!!` ;) – souldzin Mar 08 '16 at 04:19

3 Answers3

6

A Promise simplifies, and unifies the interface. Either .promisify() or .promisifyAll() will do the trick.

Then you can chain everything like this:

fs.mkdir(dir)
    .then(function success(dir) {
        ...
    })
    .catch(function failure(err) {
        ...
    })
    .finally(function () {
    });

However in node.js, the most important thing is to NOT block the I/O. It doesn't matter whether you use a Promise or a regular async/callback, as long as it's not blocking the main thread.

It's ok to have synchronous code in script that you want to run in shell, but for regular applications you should never use blocking I/O operations on purpose.

op1ekun
  • 1,918
  • 19
  • 26
  • 2
    I totally agree, IMHO promises provide much better readability and maintainability for the code which proves important in team collaboration – maurycy Mar 08 '16 at 10:12
1

I would definitely update your code to be consistent. If possible, call mkdirAsync instead of mkdir

Example (from OP's code):

var fs = Promise.promisifyAll(require('fs'));

// ...

fs.mkdirAsync(folder)
   .catch({ code: 'EEXIST' }, function(e){
       // don't care about this error code  
   })
   .catch(function(e) {
       console.log('Error to create  folder: ' + e);
   })
   .then(function(){
       require.cache.per.mk = true;
       next();
   });
souldzin
  • 1,428
  • 11
  • 23
0
Promise.promisifyAll(fs);

return fs.mkdirAsync(dir1)
  .then(function() {
    return fs.mkdirAsync(dir2);
  })
  .then(function() {
    return fs.mkdirAsync(dir3);
  })

Hope this helps.

Vinod Tigadi
  • 859
  • 5
  • 12