G
G
gleendo2017-05-11 15:33:16
Node.js
gleendo, 2017-05-11 15:33:16

Why is part of the script executed after the timer fires?

Why is all the text "Hello world!Hello is done!" displayed? after 5 seconds of the timer, instead of first "Hello world!" and then after 5 seconds "Hello is done!"?

let http = require(`http`);

let app = http.createServer((req, res) => {
  res.writeHead(200);

  res.write(`Hello world!`);

  setTimeout(() => {
    res.write(`Hello is done!`);

    res.end();
  }, 5000);
}).listen(8080);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-05-11
@Sanasol

because http doesn't work that way.

R
rustler2000, 2017-05-11
@rustler2000

```
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
res.setHeader('Transfer-Encoding', 'chunked');
res.writeHeader(200);
```

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question