Javascript 如何在承诺之前添加附加逻辑
我使用下面的代码工作正常,现在我需要在readFileAsync之前添加另一个查询目录的方法,我的问题是如何做到这一点 这是当前正在工作的代码Javascript 如何在承诺之前添加附加逻辑,javascript,node.js,promise,bluebird,Javascript,Node.js,Promise,Bluebird,我使用下面的代码工作正常,现在我需要在readFileAsync之前添加另一个查询目录的方法,我的问题是如何做到这一点 这是当前正在工作的代码 var filePath = path.join(__dirname, '../userpath'); return fs.readFileAsync(filePath, 'utf8') .then(pars.getEx.bind(null, 'user')) .then(process.bind(nul
var filePath = path.join(__dirname, '../userpath');
return fs.readFileAsync(filePath, 'utf8')
.then(pars.getEx.bind(null, 'user'))
.then(process.bind(null, 'exec'))
.then(function (result) {
return result.stdout;
}, function (error) {
return error;
});
现在我需要在前面添加一些流程,如下所示:
现在应该返回readfileAsync的路径(relFilePath)
我该怎么做
var filePath = path.join(__dirname, '../../../../employess');
fs.readdir(filePath, function (err, files) {
files.forEach(function (file) {
if (file.indexOf('emp1') === 0) {
// this should be returned
var relFilePath = filePath + '/' + file + '/unpath.txt';
console.log(relFilePath);
fs.readFile(relFilePath,'utf8', function read(err, data) {
if (err) {
throw err;
}
console.log(data);
});
}
});
}))
我使用蓝鸟…只需从中做出承诺,并在您已有的代码之前将其链接
var filePath = path.join(__dirname, '../../../../employess');
fs.readdirAsync(filePath)
.then(function(files) {
for (var i=0; i<files.length; i++)
if (file.indexOf('emp1') === 0)
return filePath + '/' + file + '/unpath.txt';
throw new Error("no file with emp1 found");
})
.then(function(relFilePath) {
return fs.readFileAsync(relFilePath, 'utf8');
})
.then(pars.getEx.bind(null, 'user'))
.then(process.bind(null, 'exec'))
.then(function (result) {
return result.stdout;
}).then(function(data) {
console.log(data);
}, function (error) {
console.error(error.message);
});
var filePath=path.join(uu dirname,'../../../../../employess');
fs.readdirAsync(文件路径)
.then(函数(文件){
对于(var i=0;i只需从中做出承诺,并在您已有的代码之前将其链接起来
var filePath = path.join(__dirname, '../../../../employess');
fs.readdirAsync(filePath)
.then(function(files) {
for (var i=0; i<files.length; i++)
if (file.indexOf('emp1') === 0)
return filePath + '/' + file + '/unpath.txt';
throw new Error("no file with emp1 found");
})
.then(function(relFilePath) {
return fs.readFileAsync(relFilePath, 'utf8');
})
.then(pars.getEx.bind(null, 'user'))
.then(process.bind(null, 'exec'))
.then(function (result) {
return result.stdout;
}).then(function(data) {
console.log(data);
}, function (error) {
console.error(error.message);
});
var filePath=path.join(uu dirname,'../../../../../employess');
fs.readdirAsync(文件路径)
.then(函数(文件){
对于(var i=0;i