I've been struggling with JavaScript promises for the past few hours, trying to fix a problem that I just can't seem to solve. My knowledge of promises is limited, so I'm open to the possibility that my approach might be incorrect.
Currently, I'm working on an app that involves a two-step process:
- Connecting to an external PaaS service, which returns a promise
- Retrieving some data within that promise
Here's a snippet of the factory I have created:
app.factory('serviceFactory', [
function() {
var getData = function getData() {
service.connect(apiKey).then(function() {
service.getData('dataStore').then(function(result) {
// Retrieve data
return result;
}, errorFunction);
},
errorFunction);
};
return {
getData: getData
};
}
]);
In this code, there are nested promises. The issue arises when I attempt to use the data from the innermost promise in an AngularJS view. Specifically, I want to incorporate the data into an ng-repeat
statement. However, no matter what I try, the data doesn't display. I've even tried assigning the data within the promise instead of returning it, like this:
service.getData('dataStore').then(function(result) {
// Retrieve data
// Assigned the enclosing scope's this to 'self'
self.data = result;
}, errorFunction);
Unfortunately, this method also fails to work. I've experimented with various other approaches, but I just can't figure out how to get the data to appear in the view. Despite successfully displaying the data with a console.log(data)
call, indicating that the data is indeed being retrieved properly. Has anyone encountered and resolved a similar issue?