-
Notifications
You must be signed in to change notification settings - Fork 0
Homework #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| const request = require('request-promise'); | ||
|
|
||
| console.log('start ...'); | ||
|
|
||
| /** | ||
| * Get repos with their contributors | ||
| * @param {string} organization | ||
| * @param {int} limit | ||
| */ | ||
| async function getContributors(organization, limit = 3) { | ||
| async function getContributorsOfReposByOwner(owner, repo) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. async function everytime returns promise. Here await unpacks promise, but async packs value in promise. |
||
| return await request({ | ||
| "method":"GET", | ||
| "uri": `https://api.github.com/repos/${owner}/${repo}/contributors`, | ||
| "json": true, | ||
| "headers": { | ||
| "User-Agent": "request-promise" | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| let contributors = {}; | ||
| let repos = (await request({ | ||
| "method":"GET", | ||
| "uri": `https://api.github.com/orgs/${organization}/repos`, | ||
| "json": true, | ||
| "headers": { | ||
| "User-Agent": "request-promise" | ||
| } | ||
| })).slice(0, limit); | ||
|
|
||
| for(i in repos) { | ||
| contributors[repos[i].name] = (await getContributorsOfReposByOwner( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With await, we will wait when is finished a previous request to start next request. |
||
| repos[i].owner.login, | ||
| repos[i].name | ||
| )).map((contributor) => contributor.login); | ||
| } | ||
|
|
||
| return contributors; | ||
| } | ||
|
|
||
| getContributors('nodejs') | ||
| .then((contributors) => { | ||
| console.log(contributors); | ||
| console.log('end'); | ||
| } | ||
| ) | ||
| .catch( | ||
| err => console.log('Something wrong happened') | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const request = require('request-promise'); | ||
|
|
||
| console.log('start ...'); | ||
|
|
||
| /** | ||
| * Get repos with their contributors | ||
| * @param {string} organization | ||
| * @param {int} limit | ||
| */ | ||
| function getContributors(organization, limit = 3) { | ||
| return new Promise(function(resolve) { | ||
| function getContributorsOfReposByOwner(owner, repo) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move this function outside |
||
| return request({ | ||
| "method":"GET", | ||
| "uri": `https://api.github.com/repos/${owner}/${repo}/contributors`, | ||
| "json": true, | ||
| "headers": { | ||
| "User-Agent": "request-promise" | ||
| } | ||
| }).then((result) => [repo, result]); | ||
| } | ||
|
|
||
| let contributors = {}; | ||
| request({ | ||
| "method":"GET", | ||
| "uri": `https://api.github.com/orgs/${organization}/repos`, | ||
| "json": true, | ||
| "headers": { | ||
| "User-Agent": "request-promise" | ||
| } | ||
| }).then((repos) => { | ||
| Promise.all(repos.slice(0, limit).map((repo) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to return Promise.all and then you can use |
||
| return getContributorsOfReposByOwner( | ||
| repo.owner.login, | ||
| repo.name | ||
| ); | ||
| })).then((allresult) => { | ||
| allresult.map(([repo, contributorsOfReposByOwner]) => { | ||
| contributors[repo] = contributorsOfReposByOwner.map((data) => data.login); | ||
| }); | ||
| resolve(contributors); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| getContributors('nodejs') | ||
| .then((contributors) => { | ||
| console.log('final result'); | ||
| console.log(contributors); | ||
| console.log('end'); | ||
| } | ||
| ) | ||
| .catch( | ||
| err => console.log('Something wrong happened') | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to move this function outside