From 3b7a11a85a27b8ffd220e75121c81bb674c2f983 Mon Sep 17 00:00:00 2001 From: Ozi Date: Tue, 20 Feb 2018 21:11:04 +0700 Subject: [PATCH] release 2 --- index.js | 31 ++++++++++++++++++++++++++----- package-lock.json | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 package-lock.json diff --git a/index.js b/index.js index 6c6a31f..92462e2 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,38 @@ const fs = require('fs'); var sleep = require('sleep'); -function readFilePromise() { +function readFilePromise(file) { // psst, the promise should be around here... + return new Promise((resolve,reject)=>{ + fs.readFile(file,'utf8',(err,data)=>{ + err ? reject(`error at:${file}`) : resolve(JSON.parse(data)); + }); + }); } function matchParentsWithChildrens(parentFileName, childrenFileName) { // your code here... (p.s. readFilePromise function(s) should be around here..) + return new Promise((resolve,reject)=>{ + readFilePromise(parentFileName).then((parent_data)=>{ + readFilePromise(childrenFileName).then((children_data)=>{ + parent_data.map((v,i,a)=>{ + v.children =[] + }); + parent_data.forEach((v)=>{ + children_data.forEach((v2)=>{ + if(v.last_name === v2.family)v.children.push(v2.full_name); + }); + }); + sleep.sleep(5); + resolve(parent_data); + }).catch((err)=> console.log(err)) + }).catch((err)=> console.log(err)) + }) } -matchParentsWithChildrens('./parents.json', './childrens.json'); -console.log("Notification : Data sedang diproses !"); +matchParentsWithChildrens('./parents.json', './childrens.json').then((data)=> console.log(data)); +// 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'); \ No newline at end of file +// matchParentsWithChildrens('./parents.json', './AGNY_a_real_file.json') +// matchParentsWithChildrens('./not_a_real_file.json', './also_not_a_real_file.json'); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b372149 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "callback_readfile", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "nan": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.8.0.tgz", + "integrity": "sha1-7XFfP+neArV6XmJS2QqWZ14fCFo=" + }, + "sleep": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/sleep/-/sleep-5.1.1.tgz", + "integrity": "sha1-h4+h1E0I7rDyb7IBjvhinrGjq5Q=", + "requires": { + "nan": "2.8.0" + } + } + } +}