Last active
April 27, 2020 14:56
-
-
Save colevandersWands/516bac40cf35ebff98535cb5d13e04fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// require dependencies | |
const fs = require('fs'); | |
const path = require('path'); | |
const nodeFetch = require('node-fetch'); | |
const assert = require('assert'); | |
// declare constants | |
const START = Date.now(); | |
const NOW = () => `${Date.now() - START} ms: `; | |
const REPORT_NAME = path.basename(__filename).replace('.js', '-report.txt'); | |
// define logging function | |
const log = (msg) => { | |
const cleanedMsg = msg.split(__dirname).join(' [ ... ] '); | |
console.log(cleanedMsg); | |
fs.appendFileSync('./' + REPORT_NAME, cleanedMsg + '\n'); | |
}; | |
// (re)initialize exercise report | |
fs.writeFileSync(REPORT_NAME, ''); | |
log((new Date()).toLocaleString()); | |
// begin main script | |
const URL = 'https://jsonplaceholder.typicode.com/comments?postId=1'; | |
log(NOW() + 'fetching ' + URL + ' ...'); | |
nodeFetch(URL) | |
.then(res => { | |
clearInterval(dotDotDot); | |
log(NOW() + 'parsing response ...'); | |
return res.json() | |
}) | |
.then(data => { | |
log(NOW() + 'testing data ...') | |
for (let i = 0; i < comments.length; i++) { | |
log(NOW() + '... entry ' + i) | |
try { | |
assert.strictEqual(comments[i].postId, 3); | |
} catch (err) { | |
log(err.stack + '\n'); | |
} | |
// or more simply | |
assert.strictEqual(comments[i].postId, 3); | |
}; | |
log(NOW() + '... done!'); | |
}) | |
.catch(err => error(err)); | |
const dotDotDot = setInterval(() => { | |
log(NOW() + '...'); | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment