Here is a straightforward code snippet for you to consider:
function colorPromise() {
return $q.when({data:['blue', 'green']})
}
function getColors() {
return colorPromise().then(function(res) {
console.log('getColors', res)
// perform some action with res
};
}
function testGetColors() {
getColors().then(function(res) {
if (angular.equals(res, {data:['blue', 'green']})) {
console.log('passes')
}
});
}
Explore this Plunker: http://plnkr.co/edit/LHgTeL9sDs7jyoS7MJTq?p=preview
In the provided example, the value of res
in the testGetColors
function happens to be undefined
.
What can be done to successfully pass res
to the second then
function within the $q
promise?