I am trying to sort an array of GitHub commit dates that I am pulling from the GitHub API with an $.ajax request. I have successfully gotten the data I want, which is returned from my function in the form of a promise.
However when I try to use .then to access the value of the promise, any array methods I use (like sort) have no effect. See code below:
// This works fine
async function getAllCommitDates() {
const commitDates = [];
const userRepos = await getRepoNames();
const repoNames = userRepos.map((repo) => repo.name);
repoNames.forEach(async (repo) => {
const commits = await getRepoCommits(repo);
commitDates.push(...getDates(commits));
});
return commitDates;
}
// Here is the problem
getAllCommitDates().then(function (res) {
// Returns the expected array of date strings in "YYYY-MM-DD" format
console.log(res);
// Returns array in the exact same order, no sorting
console.log(res.sort());
});
When I take a sample array of dates ie. ["2020-06-17", "2020-06-25", "2019-03-26", "2020-06-15"], and use .sort() it works fine. So I assume it has something to do with promises and the .then(), but what that problem is is beyond me.
Appreciate any help!
(EDIT) Adding image with log. The array from line 42 & 43 have the same contents, in the same order.