Node.js - API Promises using the 'got' or the 'async-get-json' package

Published by in January, 2017

Categorized in: Dev Stuff

Tags: Node.js  npm  

There are a few notable npm packages that handle promises with Node.js when retrieving apis. Here's two that I use and in the following snippets Im pinging github's api and returning the promised resolve data.

Using the 'got' npm package:

var got = require('got');
var terms = ['Brainf**k', 'Velato', 'Ook!'];var promises = terms.map(term => got('https://api.github.com/search/repositories?q=' + term));Promise.all(promises).then(function (data) {
console.log(data);
}).catch(function(error) { console.log(error) });

Using the 'async-get-json' npm package:

var getJSON = require("async-get-json");
var terms = ['Brainf**k', 'Velato', 'Ook!'];
var promises = terms.map(term => getJSON('https://api.github.com/search/repositories?q=' + term));
await Promise.all(promises);

Both methods above get the job done. Happy trails!