Ok, lets refactor our last post.
There was only one reason we use the new Promise()
constructor, and that was because the request is Async. In fact I made a slight mistake really as some errors won't be handled in the correct way. What I should have done is move the new Promise()
to just before the request.
Now one great thing about Promises is that error's will propagate down the chain, in our example so far we have a console.log() inside our request object. What would be nice is that all un-handled errors are handled in one place. In this example the run() function seems a nice place.
If you now look at your createInsta function you would think it's using all sync function, but in fact apart from existsSync everything else is async. Like mentioned in the comments, existSync really wants removing anyway,. Instead just create the directory and catch the error in a try catch, or even better make a utility function say called tryMkDir, that does this (promise based of course).. :)
One last note, await / async is a pretty new javascript, I think it's ES7, and I don't believe it's yet even finalised. But you can use it now, by using something like Babel.
//lets make a Promise based request.
//There is an NPM module that would do this and more
//but this is a good example of making existing callback
//functions into a Promise.
async function promRequest(url) {
return new Promise(function(resolve, reject) {
request(url, function(error, response, body) {
if (error) return reject(error);
//promises can only return one thing
//so use an object literal to return more
resolve({
response:response,
body: body
});
});
});
}
//lets also promisify some Node stuff.
var Promise = require('bluebird');
//there is a promisifyall, but I like to be
//specific about what I promisify.
var fsWriteFile = Promise.promisify(fs.writeFile),
fsReadFile = Promise.promisify(fs.readFile),
fsMkdir = Promise.promisify(fs.mkdir);
//because promRequest is already a promise
//we can now get rid of the new Promise contructor here.
//The only part of this function that is now async
//is existsSync, the exists() in node is depreciated
//due to potential race conditions, really this
//should be altered to just try and create dirctory
//and catch error if exists.
async function createInsta(email, userid) {
if (!fs.existsSync('users/' + email + '/' + userid)) {
await fsMkdir('users/' + email + '/' + userid);
await fsWriteFile(('users/' + email + '/' + userid + '/instagram.json'), await fsReadFile("data/data.json", "utf-8"));
console.log("insta folder created");
}
console.log("Initializing Data")
var data = await fsReadFile('users/' + email + '/' + userid + '/instagram.json', "utf-8")
data = JSON.parse(data);
var res = JSON.parse(
await promRequest("https://instagram.com/" + userid + "/?__a=1").body);
var result = res.user.followed_by.count
data.STARTING_COUNTS[0].DAY = result;
data.STARTING_COUNTS[0].WEEK = result;
data.STARTING_COUNTS[0].MONTH = result;
return data;
}
//to be able to use await our function also needs to be async
async function run() {
//lets catch all unahandled errors here.
try {
var data = await createInsta('bob@bob.com');
} catch (e) {
//fantasic any error's can now be logged
//eg. fsMkdir might have failed and it's still
//going to be logged here..
console.log(e);
}
}
while (result === undefined) {deasync.sleep(100) }
And why is it needed?