Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promise chaining achieved #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions index-chaining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
var sleep = require('sleep');

function readFilePromise(fileName) {
return new Promise((resolve,reject) => {
fs.readFile(fileName, 'utf8', (err,data)=>{
if (err) {
reject(err);
} else {
resolve(data);
}
})
})
}

function matchParentsWithChildrens(parentFileName, childrenFileName) {
// read parents
let parent_data =[];
readFilePromise(parentFileName)
.then((data)=>{
sleep.sleep(5);
parent_data = JSON.parse(data);
console.log(`file ${parentFileName} read!`);
// read childrens data
return readFilePromise(childrenFileName)
})
.then((data2)=>{
sleep.sleep(5);
let children_data = JSON.parse(data2);
console.log(`file ${childrenFileName} read!`);

for (let i = 0; i < parent_data.length; i++) {
parent_data[i].childrens = [];
for (let j = 0; j < children_data.length; j++) {
if (parent_data[i].last_name === children_data[j].family) {
parent_data[i].childrens.push(children_data[j].full_name);
}
}
}
console.log(parent_data);

})
.catch((err)=>{
console.log(err);
console.log('Terjadi error pada proses pembacaan data');
});
}


matchParentsWithChildrens('./parents.json', './childrens.json');
console.log("Notification : Data sedang diproses !");

// for Release 2
// matchParentsWithChildrens('./parents.json', './not_a_real_file.json');
// matchParentsWithChildrens('./not_a_real_file.json', './also_not_a_real_file.json');
55 changes: 55 additions & 0 deletions index-nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
var sleep = require('sleep');

function readFilePromise(fileName) {
return new Promise((resolve,reject) => {
fs.readFile(fileName, 'utf8', (err,data)=>{
if (err) {
reject(err);
} else {
resolve(data);
}
})
})
}

function matchParentsWithChildrens(parentFileName, childrenFileName) {
// read parents
readFilePromise(parentFileName).then((data)=>{
sleep.sleep(5);
let parent_data = JSON.parse(data);
console.log(`file ${parentFileName} read!`);
// read childrens data
readFilePromise(childrenFileName).then((data2) => {
sleep.sleep(5);
let children_data = JSON.parse(data2);
console.log(`file ${childrenFileName} read!`);

for (let i = 0; i < parent_data.length; i++) {
parent_data[i].childrens = [];
for (let j = 0; j < children_data.length; j++) {
if (parent_data[i].last_name === children_data[j].family) {
parent_data[i].childrens.push(children_data[j].full_name);
}
}
}

console.log(parent_data);

}).catch((err)=>{
console.log(err);
console.log('Terjadi error pada proses pembacaan data');
});

}).catch((err)=>{
console.log(err);
console.log('Terjadi error pada proses pembacaan data');
});
}

matchParentsWithChildrens('./parents.json', './childrens.json');
console.log("Notification : Data sedang diproses !");

// for Release 2
// matchParentsWithChildrens('./parents.json', './not_a_real_file.json');
// matchParentsWithChildrens('./not_a_real_file.json', './also_not_a_real_file.json');
43 changes: 43 additions & 0 deletions index-promise-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
var sleep = require('sleep');

function readFilePromise(fileName) {
return new Promise((resolve,reject) => {
fs.readFile(fileName, 'utf8', (err,data)=>{
if (err) {
reject(err);
} else {
resolve(data);
}
})
})
}

function matchParentsWithChildrens(parentFileName, childrenFileName) {
let dataPromises = [];
dataPromises.push(readFilePromise(parentFileName))
dataPromises.push(readFilePromise(childrenFileName))
Promise.all(dataPromises).then((values)=>{
let parent_data = JSON.parse(values[0]);
let children_data = JSON.parse(values[1]);

for (let i = 0; i < parent_data.length; i++) {
parent_data[i].childrens = [];
for (let j = 0; j < children_data.length; j++) {
if (parent_data[i].last_name === children_data[j].family) {
parent_data[i].childrens.push(children_data[j].full_name);
}
}
}
console.log(parent_data);

}).catch((err)=>{console.log(err)});
}


matchParentsWithChildrens('./parents.json', './childrens.json');
console.log("Notification : Data sedang diproses !");

// for Release 2
// matchParentsWithChildrens('./parents.json', './not_a_real_file.json');
// matchParentsWithChildrens('./not_a_real_file.json', './also_not_a_real_file.json');
17 changes: 0 additions & 17 deletions index.js

This file was deleted.

21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.