Skip to content

Commit 28cc85c

Browse files
committed
module caching
1 parent 7915ff7 commit 28cc85c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Modules/module-caching.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Loading request module
2+
const { send } = require("./Create-Module/request");
3+
4+
console.log(`Cache check after send function loading`);
5+
console.log(require.cache);
6+
7+
const { read } = require("./Create-Module/response");
8+
9+
// Loading request module again.
10+
const { REQUEST_TIMEOUT } = require("./Create-Module/request");
11+
12+
console.log(`Cache check after REQUEST_TIMEOUT loading`);
13+
console.log(require.cache);
14+
15+
// Even after trying to load request module twice here,
16+
// the console.log(module) in request file will be logged only once.
17+
// This happens cause of the cache mechanism of node
18+
19+
function makeRequest(url, data) {
20+
send(url, data);
21+
return read();
22+
}
23+
24+
const responseData = makeRequest("https:www.google.com", "Hello World");
25+
26+
setTimeout(() => {
27+
console.log(responseData);
28+
}, REQUEST_TIMEOUT);
29+
30+
// Check cached modules
31+
console.log(require.cache);
32+
33+
// Logging before and after loading `send` & `REQUEST_TIMEOUT` will clear that,
34+
// the complete `request` module with alll its exports gets cached by node,
35+
// even though the `send` function & `REQUEST_TIMEOUT` variable has been loaded separately

0 commit comments

Comments
 (0)