我使用Koa2
框架与Nodejs 7
和本地异步/等待功能。我试图在promise解析后为结果呈现模板(koa-art-template
模块)。如何使用异步/等待与承诺答复?
const app = new koa()
const searcher = require('./src/searcher')
app.use(async (ctx) => {
const params = ctx.request.query
if (ctx.request.path === '/') {
searcher.find(params).then((items) => {
await ctx.render('main', { items })
})
}
})
我想等待通过searcher
模块获取的物品,但兴亚给了我错误
await ctx.render('main', { items })
^^^
SyntaxError: Unexpected identifier
如果我将指日可待searcher.find(params).then(...)
,应用程序会工作,但不会等待项目。
此代码不会等待太物品:( – mikatakana
您使用的搜索器包是哪个?这不是[这个](https://www.npmjs.com/package/searcher)。 – robertklep
不,这是本地模块 – mikatakana