Skip to content

Instantly share code, notes, and snippets.

@albacoretuna
Created October 8, 2016 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albacoretuna/d7cb68c7149cc355ac37478f206202b0 to your computer and use it in GitHub Desktop.
Save albacoretuna/d7cb68c7149cc355ac37478f206202b0 to your computer and use it in GitHub Desktop.
Async/Await and Prmise.all example from http://stackabuse.com/node-js-async-await-in-es7/
"use strict";
const request = require('request-promise');
const headers = {
'User-Agent': 'scottwrobinson'
};
const repos = [
'scottwrobinson/camo',
'facebook/react',
'scottwrobinson/twentyjs',
'moment/moment',
'nodejs/node',
'lodash/lodash'
];
const issueTitles = [];
async function main() {
let reqs = repos.map(async function(r) {
let options = { url: 'https://api.github.com/repos/' + r, headers: headers };
let body = await request.get(options);
let json = JSON.parse(body);
if (json.has_issues) {
let issuesOptions = { url: 'https://api.github.com/repos/' + r + '/issues', headers: headers };
let ibody = await request.get(issuesOptions);
let issuesJson = JSON.parse(ibody);
if (issuesJson[0]) {
issueTitles.push(issuesJson[0].title);
}
}
});
await Promise.all(reqs);
}
main();
console.log(JSON.stringify(issueTitles));
// results in : []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment