Very much a nodejs noob, trying to make sense of promises, await, async. I promise you I did my due diligence research (spent the entire day studying to get the code hereunder. I'm still not entirely sure everything is as it should be and can find no reference that has the exact same thing (or close enough) to what I'm trying to do here.
Thanks for any help.
General structure:
function msg() is waiting for 4 functions to complete, the 4 api calls (code only shows one): function redditPromise().
redditPromise() calls async function redditGet() -> that's the one that will call reddit API and in the meantime save the API data to database. (function saveToDb())
var nodeSocialApi = require('node-social-api');
var Socstat = require('../proxy').Socstat;
exports.index = function (req, res, next) {
/* SAVES DATA TO MONGODB */
function saveToDb(website,total) {
//** Start : NewAndSave
Socstat.newAndSave(website, total, "random", function (err, socstat) { // newAndSave -> proxy/socstat.js
if (err) {
return next(err);
}
});
//** End : NewAndSave
}
/* END SAVES DATA TO MONGODB */
/* GET DATA FROM REDDIT API */
const reddit = new nodeSocialApi.Reddit(); // no auth needed
async function redditGet() {
let result;
await reddit.get('r/about/about.json')
.then((data) => {
// callback value for promise
result = data.data.subscribers;
saveToDb("reddit",result);
}) // end then
.catch(err => console.log(err));
return result;
}
/* END : GET DATA FROM REDDIT API */
/* REDDIT PROMISE (all the others look the same) */
function redditPromise() {
return new Promise(resolve => {
resolve(redditGet());
});
}
/* END : REDDIT PROMISE (all the others look the same) */
/* ONE FUNCTION THAT WAITS FOR ALL PROMISED FUNCTIONS */
async function msg() {
const [a, b, c,d] = await Promise.all([githubPromise(), twitterPromise(), redditPromise(), facebookPromise()]);
console.log(a + " " + b + " " + c + d);
}
/* END: ONE FUNCTION THAT WAITS FOR ALL PROMISED FUNCTIONS */
msg();
}; // END exports