Created
August 7, 2018 06:24
-
-
Save winguse/fe34f9818732d6bd53cc7ecef8d61ea6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* ------------ monkey patching ------------ */ | |
| const http = require('http') | |
| const originalServerEmit = http.Server.prototype.emit; | |
| http.Server.prototype.emit = function (event) { | |
| if (event === 'request') { | |
| // setup the context here as the root of the execution tree | |
| setContext(); | |
| } | |
| return originalServerEmit.apply(this, arguments) | |
| }; | |
| /* ------------ business logic ------------ */ | |
| async function even_more_nested_call() { | |
| console.log("nested in nested.", getContext()); | |
| } | |
| async function nested_call() { | |
| console.log("nested call", getContext()); | |
| await even_more_nested_call(); | |
| } | |
| async function some_other_nested_call() { | |
| console.log("some other nested call", getContext()); | |
| } | |
| async function do_work() { | |
| console.log("do some work", getContext()); | |
| await nested_call(); | |
| await some_other_nested_call(); | |
| } | |
| const server = http.createServer(function (req, res) { | |
| do_work().then(() => { | |
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| res.write('Hello World!'); | |
| res.end(); | |
| }); | |
| }); | |
| server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment