Skip to content

Instantly share code, notes, and snippets.

@sudhakar3697
Created September 27, 2019 14:26
Show Gist options
  • Select an option

  • Save sudhakar3697/b41daca06b06e08c2a2e38392b76e27a to your computer and use it in GitHub Desktop.

Select an option

Save sudhakar3697/b41daca06b06e08c2a2e38392b76e27a to your computer and use it in GitHub Desktop.
const express = require('express')
const app = express()
const port = 2709
app.get('/home', (req, res) => res.send('Hello Zetwerk'))
app.get('/test', async (req, res) => {
await doSomeLongOperation();
res.send('Operation is done')
})
app.use(function (req, res, next) {
res.status(404).send('Sorry can\'t find that!')
})
async function doSomeLongOperation() {
for (let i = 0; i < 5000000000000; i++) {
console.log(await square(i))
}
}
async function square(i) {
return new Promise(resolve => setTimeout(function () {
resolve(i*i)
}, 1000)
)
}
app.listen(port, () => console.log(`App listening on port ${port}`))
@sudhakar3697

sudhakar3697 commented Sep 27, 2019

Copy link
Copy Markdown
Author

If we want to do some complex operation instead of the square function, we can offload it by using worker threads | child process | cluster | N-API native node addons (C/C++).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment