I have a local SQL table called Idioms and a table called Favorites. Favorites only has one column, an idiom_id column. I can check and see if a user has favorited an idiom by running a query like so.
You want to return a promise, because you need to tell whatever is calling this service to wait until you’ve finished. You could certainly change what your promise resolves with using a similar method to above:
isFavorited(idiomId, database): Promise<boolean> {
return database.executeSql(`SELECT * FROM Favorites WHERE idiom_id=${idiomId}`)
.then(results => results.rows.length > 0);
}
So now you return a promise, but a promise that resolves with a boolean. This is what you want, because you need to then in your controller be able to wait for this call to complete, so you can then chain the promise, or use async await.
Edit: In the code you posted above, you would always return false, because you return check before the query completes.
Also, probably don’t pass the database into each function, keep the database in the service itself. Open the connection on app start and set it to a private property of the database service. Otherwise things are going to get really ugly.
The error message in your original post Property 'toString' does not exist on type 'void' makes perfect sense, your isFavorited function does not have a return statement. The callback you pass to your promise .then has a return statement, but isFavorited does not return anything, so typescript returns void and you get your error message.
If you use the sample I poseted before, your code will work by simply using async/await or regular promises. I’ll show async/await since it’s simpler:
Thanks. I’m new to using promises (I’m just thinking of them in the same as AJAX requests. Is that wrong?). I’m not quite clear on the return value though. Is it a boolean or a promise? I want to be able to write something like this
Promises are used with ajax, anything async uses them. I’d recommend reading up on it. The return value, Promise<boolean>, is a promise that resolves with a boolean value. I’m not really going to explain the concept via a post here, but do some googling and you’ll get some better ideas. The short version is anything that takes a long time (in computer time), like getting data over the internet or writing to a hard drive, is done out of sync with the rest of your code. Javascript says “this is going to take a while, I’m going to keep running the rest of your code, when this slow task finishes, I’ll do whatever is in the .then callback.”
So, you have to do all your async stuff with either promises or callbacks, and promises are far easier to work with provided you avoid the major promise anti-patterns. Async/await makes it even easier. But ultimately it’s something you’re going to want to learn if you want to continue to develop in JS.