question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

when middleware meet setTimeout, ctx.body don't work ?

See original GitHub issue
app.use((ctx, next) => {  
  console.log('1')
  setTimeout(function(){

    console.log('2')
    return next().then(() => {
      // after
      console.log('3')
    })
  }, 1000)  
});


// response
app.use(ctx => {
  console.log('业务逻辑处理')
  ctx.body = 'i am body'
});

when visit

$ curl http://127.0.0.1:3000
Not Found%                  

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

10reactions
chentsulincommented, May 18, 2016

You should return Promise in middleware

app.use((ctx, next) => {  
  console.log('1');
  return new Promise(resolve => {
    setTimeout(resolve, 1000);
  })
  .then(() => {
      console.log('2');
      return next().then(() => {
        // after
        console.log('3');
      });
  });
});

// response
app.use(ctx => {
  console.log('业务逻辑处理');
  ctx.body = 'i am body';
});

Much better with async await:

app.use(async (ctx, next) => {  
  console.log('1');
  await new Promise(resolve => {
    setTimeout(resolve, 1000);
  });
  console.log('2');
  await next();
  // after
  console.log('3');
});

// response
app.use(ctx => {
  console.log('业务逻辑处理');
  ctx.body = 'i am body';
});
3reactions
juliangrubercommented, May 19, 2016

random your mum joke

Read more comments on GitHub >

github_iconTop Results From Across the Web

Respond to Koa request immediately, continue middleware ...
The server hitting my endpoint has very strict requirements about webhook response time, and I have heavy (very heavy) work to be done...
Read more >
Timers in Node.js
The timeout interval that is set cannot be relied upon to execute after that exact number of milliseconds. This is because other executing...
Read more >
Make resilient Go net/http servers using timeouts, deadlines ...
If the operation does not complete in the given time limit, a timeout occurs, and the operation is canceled. Initializing a net/http server ......
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
A key-value cache that Apollo Server uses to store previously encountered GraphQL operations (as DocumentNode s). It does not store query results.
Read more >
middleware - Go Packages
Compress is a middleware that compresses response body of a given ... ctx after a given timeout and return a 504 Gateway Timeout...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found