
Asynchronous stack traces
The asynchronous nature of JavaScript affects the way a stack trace works. In JavaScript, each tick (each time the JavaScript event-loop iterates) has a new stack.
Let's copy our app folder to async-stack-app:
$ cp -fr app async-stack-app
Now let's alter content.js like so:
function content (opts, c = 20) { function produce (cb) { if (--c) setTimeout(produce, 10, cb) cb(null, opts.ohoh) } return produce } module.exports = content
Then let's alter routes.js in the following way:
const content = require('./content') const {Router} = require('express') const router = new Router() router.get('/', (req, res) => { content()((err, html) => { if (err) { res.send(500) return } res.send(html) }) }) module.exports = router
Now we start our server:
$ node index.js
And make a request:
$ curl http://localhost:3000/
We'll see only a small stack trace descending from timeout specific internal code, as in the following screenshot:

We can obtain asynchronous stack traces with the longjohn module. Let's install it as a development dependency:
$ npm install --save-dev longjohn
Now we can add the following the very top of the index.js file:
if (process.env.NODE_ENV !== 'production') { require('longjohn') }
Let's run our server again:
$ node index.js
And make a request:
$ curl http://localhost:3000/
Now we should see the original stack, followed by a line of dashes, followed by the call stack of the previous tick.
