如何在承诺之前添加附加逻辑

我使用下面的代码,它工作正常,现在我需要添加到它之前的readFileAsync查询Dir的另一个方法,我的问题是如何做到这一点?

这是目前正在工作的代码

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; }); 

现在我需要添加一些过程之前是这样的:现在应该返回的pathreadfileAsync(-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); });