如何使用快速应用程序内的承诺?

我试图在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逻辑封装在下游。