I'm implementing a Github Commit History Flow Visualizer in Angular with a Node backend handling webhooks and sockets. I'm using the node package deferred to handle promises and I'm running into some trouble assigning complex variables inside my function callbacks. I love promises, but this one is stumping me. See my code below:
function getGithubRepoData(repoName) {
var d = deferred();
var reponseData = {
branches: { values: [] },
tags: { values: [] },
commits: [{ values: [] }]
}
deferred(getGithubRepoBranches(repoName), getGithubRepoTags(repoName))(function(result) {
console.log("both done");
console.log(result[0]);
console.log("blocking?");
responseData.branches.values = result[0];
console.log("past here?");
responseData.tags.values = result[1];
console.log("now do commits");
getGithubRepoCommits(repoName, result[0]).then(function(result) {
responseData.commits[0].values = result;
d.resolve(responseData);
})
})
return d.promise();
}
This function is clearly meant to get the branches, tags, and commits (for all branches). The problem lies in when I try to assign responseData.branches.values
. The code executes properly until then and just hangs, outputting the following:
both done
4
blocking?
And then nothing...
Here's a simpler proof of concept you can run to illustrate my issue:
var deferred = require('deferred');
function dtest() {
var d = deferred();
setTimeout(function () {
d.resolve("good");
}, 500);
return d.promise;
}
var reponseData = {
branches: { values: [] },
tags: { values: [] },
commits: [{ values: [] }]
}
var tester = "test";
deferred(dtest())(function(result) {
tester = result;
console.log(result);
console.log("here?");
responseData.branches.values = result;
console.log("here?");
console.log(responseData.branches.values);
})
Which outputs:
good
here?
As you can see, the simple string variable is assigned with no issue, but the more complex object hangs indefinitely.
At this point my suspicion is that the package I am using has some error that I'm not seeing, but I'm hoping someone with more Node knowledge than me can swoop in and point out where I'm falling into a locking issue or some race condition. If this doesn't work I'll have to look into running each get function in order instead of parallel. Thanks in advance for the help!