Skip to content

Commit 85769fb

Browse files
author
Ashfaq
committed
react-hooks and cocurrent Api calls
1 parent cec7ef0 commit 85769fb

11 files changed

+168
-1
lines changed

.DS_Store

8 KB
Binary file not shown.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

concurrentApiCalls.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const axios = require('axios');
2+
let NewsIDs = [9224, 8917, 8952, 8884, 8887, 8869, 8958, 8940, 8908, 9005, 8873, 9671, 9067, 9055, 8865, 8881, 8872, 8955, 10403, 8903, 8928, 9125, 8998, 8901, 8902, 8907, 8894, 8870, 8878, 8980, 8934, 8943, 8876];
3+
4+
module.exports = {
5+
concurrentApiCalls: (function getNewsByID(IDs) {
6+
// It will return and URL
7+
8+
function gerURL(id) { return `https://hacker-news.firebaseio.com/v0/item/${id}.json?` }
9+
// It will make API Call and returns promise Object.
10+
async function fetchData(URL, id) {
11+
try {
12+
let apiResponse = await axios.get(URL)
13+
return { [id]: apiResponse.data.text || '' }
14+
} catch (error) { return { [id]: '' } }
15+
}
16+
// It will return an array of promise Object.
17+
function fetchCompleteData() {
18+
return IDs.map((id) => fetchData(gerURL(id), id))
19+
}
20+
// Promise.all will wait till all promises are resolved or any one is rejected.
21+
22+
return Promise.all(fetchCompleteData()).then(resp => {
23+
console.log({ success: true, data: resp })
24+
return { success: true, data: resp }
25+
}).catch(error => {
26+
return { success: false }
27+
})
28+
})(NewsIDs)
29+
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const { concurrentApiCalls } = require("./concurrentApiCalls");

package-lock.json

+43
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"commonJS"
1414
],
1515
"author": "mohammedashfaq.nitk@gmail.com",
16-
"license": "ISC"
16+
"license": "ISC",
17+
"dependencies": {
18+
"axios": "^0.19.0"
19+
}
1720
}

reactHooks/.vscode/launch.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "attach",
7+
"name": "Node: Nodemon",
8+
"processId": "${command:PickProcess}",
9+
"restart": true,
10+
"protocol": "inspector"
11+
}
12+
]
13+
}

reactHooks/hookReplica1.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function useState(initialValue) {
2+
var _val = initialValue;
3+
function getCurrentState() {
4+
return _val;
5+
}
6+
function setState(newState) {
7+
_val = newState;
8+
}
9+
return [getCurrentState, setState];
10+
}
11+
module.exports = {
12+
useState
13+
};

reactHooks/hookReplica2.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function useState(initialValue) {
2+
var _val = initialValue;
3+
4+
function setState(newState) {
5+
_val = newState;
6+
}
7+
return [_val, setState];
8+
}
9+
module.exports = {
10+
useState
11+
};

reactHooks/hookReplica3.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
MyReact: (function () {
3+
let _val;
4+
return {
5+
render(Component) {
6+
const Comp = Component()
7+
Comp.render()
8+
return Comp
9+
},
10+
useState(initialValue) {
11+
_val = _val || initialValue;
12+
13+
function setState(newState) {
14+
_val = newState;
15+
}
16+
return [_val, setState];
17+
}
18+
}
19+
})()
20+
}

reactHooks/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { useState: useState1 } = require('./hookReplica1');
2+
const { useState: useState2 } = require('./hookReplica2');
3+
const { MyReact } = require('./hookReplica3');
4+
5+
function Button(useState) {
6+
debugger;
7+
const [state, setState] = useState(15);
8+
console.log('\ninitial Value', state);
9+
setState(20);
10+
console.log('updated Value', state);
11+
console.log('execution Completed\n');
12+
}
13+
14+
15+
function Counter() {
16+
const [count, setCount] = MyReact.useState(0)
17+
return {
18+
click: () => setCount(count + 1),
19+
render: () => console.log('render:', { count })
20+
}
21+
}
22+
23+
// function hooks() {
24+
// Button(useState1);
25+
// Button(useState2);
26+
27+
// }
28+
let App
29+
App = MyReact.render(Counter) // render: { count: 0 }
30+
App.click()
31+
App = MyReact.render(Counter) // render: { count: 1 }
32+
33+
// hooks();

0 commit comments

Comments
 (0)