Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions 01_async.js
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) {
Copy link

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

Copy link

Choose a reason for hiding this comment

The 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.
More efficient return request

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(
Copy link

Choose a reason for hiding this comment

The 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.
You need to use Promise.all to make them parallelly.

const contributors = await Promis.all(repos.map(repo => ..))

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')
);

56 changes: 56 additions & 0 deletions 02_promise.js
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) {
Copy link

Choose a reason for hiding this comment

The 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) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to return Promise.all and then you can use .then((allresult) on the same level as .then((repos)

return getContributorsOfReposByOwner(
repo.owner.login,
repo.name
);
})).then((allresult) => {
allresult.map(([repo, contributorsOfReposByOwner]) => {
contributors[repo] = contributorsOfReposByOwner.map((data) => data.login);
});
resolve(contributors);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve we can use when we work with the callbacks.
Here Promise chain and Promise.all is enough.

});
});
});
}

getContributors('nodejs')
.then((contributors) => {
console.log('final result');
console.log(contributors);
console.log('end');
}
)
.catch(
err => console.log('Something wrong happened')
);
Loading