Koa: setTimeout response problem

Created on 4 May 2017  路  3Comments  路  Source: koajs/koa

This example return 404.

const Koa = require('koa');
const app = new Koa();

app.use(ctx => {

  setTimeout(function () {
    ctx.body = 'Hello Koa';
  }, 1000);

});

app.listen(3000);
question

Most helpful comment

try

app.use(ctx => new Promise(resolve =>{
  setTimeout(function () {
    ctx.body = 'Hello Koa';
        resolve()
  }, 1000);
}));

All 3 comments

try

app.use(ctx => new Promise(resolve =>{
  setTimeout(function () {
    ctx.body = 'Hello Koa';
        resolve()
  }, 1000);
}));

why

try

app.use(ctx => new Promise(resolve =>{
  setTimeout(function () {
      ctx.body = 'Hello Koa';
        resolve()
  }, 1000);
}));

owing to what? event-loop?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tvq picture tvq  路  4Comments

rainesinternationaldev picture rainesinternationaldev  路  5Comments

TheRav3n picture TheRav3n  路  3Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

coodoo picture coodoo  路  4Comments