Reputation: 1076
I have a function that runs a query (returning a promise) and then manipulates the returned data. How do I get the calling function to have access to the manipulated data?
The call is like this:
getAgeGroupList().then(function (result) {
// Would like to access manipulated data here.
});
And the function is as below. The query.find()
returns a Promise in Parse.
function getAgeGroupList(){
var query = new Parse.Query("Fixtures_and_results");
return query.find({
success: function(results) {
/* Manipulate results here, then return manipulated data */
// How do I return the manipulated data? e.g return(newArray)
},
error: function(error) {
}
});
}
Upvotes: 2
Views: 8880
Reputation: 1074355
If query.find
returns a promise, then you don't want to use success
and error
handlers; instead, use then
to manipulate the value:
function getAgeGroupList(){
var query = new Parse.Query("Fixtures_and_results");
return query.find().then(results => {
// Manipulate results here
return updatedResults;
});
}
That will return a new promise that will resolve with your manipulated results rather than the original. This is promise chaining.
If the query.find
call fails, its promise will reject, and the promise created by then
will pass that rejection along (without calling the then
handler).
I used an arrow function above, but looking at your code, it doesn't use any ES2015+ features other than promises. So you may want:
function getAgeGroupList(){
var query = new Parse.Query("Fixtures_and_results");
return query.find().then(function(results) { // <=== Changed to traditional function
// Manipulate results here
return updatedResults;
});
}
instead.
Upvotes: 6