Thursday, 25 May 2017

How to use async/await with promise response?

I'm using Koa2 framework with Nodejs 7 and native async/await functions. And I'm trying to render template (koa-art-template module) for results after promise resolves.

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

I want to wait for getting items by searcher module, but Koa gives me error

  await ctx.render('main', { items })
        ^^^
SyntaxError: Unexpected identifier

If i will set await for searcher.find(params).then(...), an application will work but won't wait for items.



via mikatakana

No comments:

Post a Comment