How to fix my promise to return here for this feature?

I have an array of tags that can contain up to 3 elements.

Next, I pass the tags to my getTagQuotes function and try to set the promise variable for it. Now I get the promise undefined.

enter image description here

 // If there are tags, the wait for promise here: if (tags.length > 0) { var promise = getTagQuotes(tags).then(function() { console.log('promise =',promise); for (var i=0; i<tweetArrayObjsContainer.length; i++) { chartObj.chartData.push(tweetArrayObjsContainer[i]); } chartDirective = ScopeFactory.getScope('chart'); chartDirective.nvd3.drawChart(chartObj.chartData); }); } else { chartDirective = ScopeFactory.getScope('chart'); chartDirective.nvd3.drawChart(chartObj.chartData); } 

^ The goal above is to make sure that the arrays in tweetArrayObjsContainer have been filled and placed in chartObj.chartData before I draw the nvd3 diagram.

Below is my getTagQuotes function, which runs another loop through tags (up to 3) and calls another GetTweetVolFactory.returnTweetVol service, which makes the actual API call to retrieve the data.

I have code that checks to see if we are in the last step of the loop, then calls the formatTagData function.

Inside formatTagData is where I populate tweetArrayObjsContainer .

 // Check for and GET tag data: //////////////////////////////////////////////////////////////////// function getTagQuotes(tags) { console.log('getTagQuotes called with',tags); var deferred = $q.defer(); // <- setting $q.defer here var url = 'app/api/social/twitter/volume/'; for (var i=0; i<tags.length; i++) { var loopStep = i; rawTagData = []; GetTweetVolFactory.returnTweetVol(url+tags[i].term_id) .success(function(data, status, headers, config) { rawTagData.push(data); if (loopStep === (rawTagData.length - 1)) { // formatTagData fills the tweetArrayObjsContainer with data: formatTagData(rawTagData); deferred.resolve(); // <-- last step, so resolve return deferred.promise; } }) .error(function(data, status) { return 'error in returning tweet data'; }); } function formatTagData(rawData) { tweetArrayObjsContainer = []; for (var i=0; i<rawData.length; i++) { var data_array = []; var loopNum = i; _(rawData[i].frequency_counts).reverse().value(); for (var j=0; j<rawData[loopNum].frequency_counts.length; j++) { var data_obj = {}; rawData[loopNum].frequency_counts[j].start_epoch = addZeroes(rawData[loopNum].frequency_counts[j].start_epoch); data_obj.x = rawData[loopNum].frequency_counts[j].start_epoch; data_obj.y = rawData[loopNum].frequency_counts[j].tweets; data_array.push(data_obj); } var tweetArrayObj = { "key" : "Quantity"+(i+1), "type" : "area", "yAxis" : 1, "color" : tagColorArray[i], "values" : data_array }; tweetArrayObjsContainer.push(tweetArrayObj); } } } 
+1
source share
2 answers

Your getTagQuotes function should return the promise using the $q service, check it .

It should look like this:

 function getTagQuotes(tags) { var deferred = $q.defer(); (...) // When the action is finished here, call resolve: deferred.resolve(); (...) // And in the end, return the promise return deferred.promise; } 

And finally, in your main code you will do:

 var promise = getTagQuotes(tags).then(function(){ console.log('promise =',promise); // Fill chartObj with tweet data arrays (...) }); 
+2
source

This function does not have a return statement.

 function getTagQuotes(tags) 
+1
source

All Articles