如何使用快速应用程序内的承诺?
我试图在app.get函数中使用promise,它将运行一个将在promise上运行的查询。 但问题是回应不等待诺言,只是回应。
任何想法如何代码应该如此承诺可以住app.get内快速应用程序?
app.get('/test', function (req, res) { db.getData() .then(function (data) { res.setHeader('Content-Type', 'text/plain'); res.end(data); }) .catch(function (e) { res.status(500, { error: e }); }); });
以下是Express文档的答案:
app.get('/', function (req, res, next) { // do some sync stuff queryDb() .then(function (data) { // handle data return makeCsv(data) }) .then(function (csv) { // handle csv }) .catch(next) }) app.use(function (err, req, res, next) { // handle error })
主要是通过.catch()
传递next
一个通用error handling路由,可以将error handling逻辑封装在下游。
- 结束一个承诺链
- EmberJS Express API | TypeError:app.route不是module.exports的函数
- 无法在node-ftp模块中进行denodeify方法
- NodeJS:未处理的承诺在方法结束时拒绝
- 使用promise来编写可读代码的最佳方法是什么?
- Node.js Web服务器fs.createReadStream vs fs.readFile?
- 使用Node.js / promisses按types捕获exception
- 如何在SailsJS / Waterline中使用Model.query()和promise?
- Promise.all与嵌套的Promise.all