试图将 Promise 集成到我的 seneca 模块中。
首先,我们有 server.js 文件,它公开了一个路由:
var express = require('express');
var app = express();
var Promise = require('bluebird');
var seneca = require('seneca')();
var act = Promise.promisify(seneca.act, {context: seneca});
var payroll = require ('./app/payroll');
seneca.use(payroll);
var router = express.Router();
router.route('/')
.get(function(req, res) {
act({role:'payroll', cmd:'generate-weekly-report-data', wc: 1489363200})
.then(function (data) {
res.json(data)
})
.catch(function (err) {
res.send(err)
});
})
app.use('/payroll', router);
app.listen(3000);
console.log("Magic happens at port 3000");
然后我们有了工资模块(payroll.js),其中包含一些会计功能的开始:
module.exports = function(options) {
var Promise = require('bluebird');
var seneca = this;
var act = Promise.promisify(seneca.act, {context: seneca});
var payStructure = require ('../plugins/pay-structure');
seneca.use(payStructure);
seneca.add({role:'payroll', cmd:'generate-weekly-report-data'}, function (args, done) {
act({role:'pay-structure', cmd:'get'})
.then(function (pay_structure) {
var referralPayRate = pay_structure.referralPayRate
var stylistPayRate = pay_structure.stylistPayRate
done( null, act({role:'transactions', cmd:'get_week', wc: args.wc, condense: true, totals: true }) )
})
.catch(function (err) {
done(err)
});
});
}
任何帮助表示赞赏。