Skip to content

Instantly share code, notes, and snippets.

@gauravmuk
Created July 7, 2019 18:14
Show Gist options
  • Save gauravmuk/999790a2ef36902181d9bbbe8c30aafa to your computer and use it in GitHub Desktop.
Save gauravmuk/999790a2ef36902181d9bbbe8c30aafa to your computer and use it in GitHub Desktop.
Async await nested deps
const fetch = require('node-fetch');
async function getDependencies(packageName) {
const url = `https://registry.npmjs.org/${packageName}/latest`;
try {
const response = await fetch(url);
const body = await response.json();
return Array.from(Object.keys(body.dependencies));
} catch (err) {
return [];
}
}
async function getAllDependencies(packageName) {
const deps = await getDependencies(packageName);
return Promise.all(deps.map(async dep => {
return await getAllDependencies(dep);
})).then(values => {
return [...new Set([...deps].concat(...values))];
});
}
getAllDependencies('forever').then(values => {
console.log(values.length);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment