admin管理员组文章数量:1279235
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 remended ? The code is working as expected...
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 remended ? The code is working as expected...
Share Improve this question edited Feb 25, 2016 at 13:08 asked Feb 25, 2016 at 10:51 user4445419user4445419 8-
1
You can just use
mkdirAsync
, promisifyAll creates it for you. – Benjamin Gruenbaum Commented Feb 25, 2016 at 14:22 - @BenjaminGruenbaum -Thanks but how should I call it with the catch and the next inside promise ,can you please provide example? – user4445419 Commented Feb 25, 2016 at 14:27
-
Exactly the same way you did for
readdir
– Benjamin Gruenbaum Commented Feb 25, 2016 at 14:43 - it is remended to use promise for an async call. so use the same way that used for readdir – Raja Sekar Commented Mar 1, 2016 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 Commented Mar 8, 2016 at 0:53
3 Answers
Reset to default 6 +25A 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();
});
Promise.promisifyAll(fs);
return fs.mkdirAsync(dir1)
.then(function() {
return fs.mkdirAsync(dir2);
})
.then(function() {
return fs.mkdirAsync(dir3);
})
Hope this helps.
本文标签: javascriptShould I use promise for mkdirStack Overflow
版权声明:本文标题:javascript - Should I use promise for mkdir - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238332a2363467.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论