4 ms·
Replying to myself: since this comment is so popular, here's a fully working version for node 8: const util = require('util'), fs = require('fs'), wr
by nailer 9y ago
Replying to myself: since this comment is so popular, here's a fully working version for node 8:
const util = require('util'),
fs = require('fs'),
writeFile = util.promisify(fs.writeFile),
request = require('superagent'),
stat = util.promisify(fs.stat),
sorts = require('sorts'),
log = console.log;
const getPhotosAndSaveThem = async function(){
try {
const response = await request.get('https://jsonplaceholder.typicode.com/photos');
const photos = response.body.sort(sorts.byKey('title'))
await writeFile('photos.json', JSON.stringify(photos, null, 2))
const status = await stat('photos.json');
log(`I got some data from an API and saved the responses at ${status.birthtime}`)
} catch (e) {
log(`Oh no, something went wrong!`, e)
}
}
getPhotosAndSaveThem()