Skip to content

Commit 8cdc957

Browse files
committed
Update nodejs-programming.md
1 parent dd11cdf commit 8cdc957

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

nodejs-programming.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,120 @@
11
# Node.js Programming Practice
2+
3+
<br/>
4+
5+
## Q. ***What will happen when that code will be executed?***
6+
7+
```js
8+
var EventEmitter = require('events');
9+
10+
var eventObj = new EventEmitter();
11+
12+
eventObj.on('event1', function () {
13+
console.log('Event1 fired!');
14+
process.nextTick(function () {
15+
eventObj.emit('event2');
16+
});
17+
});
18+
19+
eventObj.on('event2', function () {
20+
console.log('Event2 fired!');
21+
process.nextTick(function () {
22+
eventObj.emit('event3');
23+
});
24+
25+
});
26+
27+
eventObj.on('event3', function () {
28+
console.log('Event3 fired!');
29+
process.nextTick(function () {
30+
eventObj.emit('event1');
31+
});
32+
});
33+
34+
eventObj.emit('event1');
35+
```
36+
37+
Output
38+
39+
```js
40+
Event1 fired!
41+
Event2 fired!
42+
Event3 fired!
43+
...
44+
...
45+
...
46+
Event1 fired!
47+
Event2 fired!
48+
Event3 fired!
49+
```
50+
51+
## Q. ***Rewrite the code sample without try/catch block***
52+
53+
```js
54+
async function getData(req, res) {
55+
try {
56+
const a = await functionA();
57+
const b = await functionB();
58+
res.send("some result")
59+
} catch (error) {
60+
res.send(error.stack);
61+
}
62+
}
63+
```
64+
65+
**Solution:**
66+
67+
```js
68+
async function getData(){
69+
const a = await functionA().catch((error)=>console.log(error));
70+
const b = await functionB().catch((error)=>console.log(error));
71+
if (a && b) console.log("some result")
72+
}
73+
```
74+
75+
## Q. ***Consider following code snippet***
76+
77+
```js
78+
{
79+
console.time("loop");
80+
for (var i = 0; i < 1000000; i += 1) {
81+
// Do nothing
82+
}
83+
console.timeEnd("loop");
84+
}
85+
```
86+
87+
The time required to run this code in Google Chrome is considerably more than the time required to run it in Node.js Explain why this is so, even though both use the v8 JavaScript Engine.
88+
89+
**Solution:**
90+
91+
Within a web browser such as Chrome, declaring the variable `i` outside of any function\'s scope makes it global and therefore binds it as a property of the `window` object. As a result, running this code in a web browser requires repeatedly resolving the property `i` within the heavily populated `window` namespace in each iteration of the `for` loop.
92+
93+
In Node.js, however, declaring any variable outside of any function\'s scope binds it only to the module\'s own scope (not the `window` object) which therefore makes it much easier and faster to resolve.
94+
95+
## Q. ***Rewrite promise-based Node.js applications to Async/Await***
96+
97+
```js
98+
function asyncTask() {
99+
return functionA()
100+
.then((valueA) => functionB(valueA))
101+
.then((valueB) => functionC(valueB))
102+
.then((valueC) => functionD(valueC))
103+
.catch((err) => logger.error(err))
104+
}
105+
```
106+
107+
**Solution:**
108+
109+
```js
110+
async function asyncTask() {
111+
try {
112+
const valueA = await functionA()
113+
const valueB = await functionB(valueA)
114+
const valueC = await functionC(valueB)
115+
return await functionD(valueC)
116+
} catch (err) {
117+
logger.error(err)
118+
}
119+
}
120+
```

0 commit comments

Comments
 (0)