Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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