Should I use promise for mkdir
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.
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();
});