Reputation: 23
im trying to create an app using react native i created some static classes, contains globalDB and localDB which have data about the local database and the global database (the web db) now i want to compare some datam and i cant resolve my problem: i have 2 functions in 2 sepereated classes, which returns me the data of the db. now what i want is to get a JSON from them functions, but its not working, becouse the functions are async and they require callbacks, is there any options to get the json of the data? what i mean by saying those are async funcs are: from the globalDB it uses fetch, and i cant return a json from the function (it returns me a promise) and from the localDB i use react-native-db-models, which returns me a promise and not a json (i want in the localDB to have 1 function which will return me the whole counts, by doing this:
var objCounts = {};
objCounts.people = this.peopleCounts();
objCounts.example = this.exampleCounts();
etc....
)
if its not clear, ill try to explain this better: i want to create a function which checks if the local database has the same rows like the server database, so lets say this function called checkData() now, i have 2 classes: localDB and globalDB. in every one of them there are functions which should return info about the data, in localDB there are functions: getPeople, getPencils, getCounts in globalDB there are functions: getPeople, getPencils, getCounts. now, i want to check in checkData() if the getCounts are equal, i mean, ill get json like:
[
people: "5",
pencils: "6"
]
in getCounts in globalDB and in localDB (maybe not the same, depends on the data on the local, but that's what i want to check) now, i dont succeed to do it, becouse the getCounts functions returns promise.. can any1 PLEASE suggest a solution how can i reach what i want?
*UPDATE: used mike solution, i got the data from globalDB class, still got a problem with localDB class: im using react-native-db-models, and calling a function get_all which requires a callback in the parameter, any idea how to transfer it to await and async?
Upvotes: 0
Views: 4400
Reputation: 1452
Using Promises
:
Promise.all([
getPeople(),
getPencils(),
getCounts()
]).then(function (results) {
console.log(results[0]); // people JSON
console.log(results[1]); // pencils JSON
console.log(results[2]); // counts JSON
});
Using async await
:
async function getAll () {
let people = await getPeople();
let pencils = await getPencils();
let counts = await getCounts();
console.log(people); // people JSON
console.log(pencils); // pencils JSON
console.log(counts); // counts JSON
} catch (error) {
// handle errors
}
For react-native-db-models
I would try this:
(Taken from their documentation... I have not used it before and it may not work).
var App = React.createClass({
get_users: async function () {
try {
let result = await DB.users.get_all();
console.log(result);
} catch (e) {
console.error(e);
}
},
render: function(){
return (
<View>
<Text onPress={this.get_users}> Hello </Text>
</View>
);
}
});
Upvotes: 1