although I thought I got the gist of how async functions work in JS, this one does not wait correctly. Outer function, handling a social media like button press:
if (!postLiked){
likePost (uid, cardData)
.then (getPostLiked(uid))
.catch (error => console.log (error))
}
Now the likePost function, updating data on the firebase realtime database:
export function likePost(uid, cardData) {
return new Promise((resolve, reject) => {
//do stuff with the data...
set(ref(db, `${cardData.path}/likeData`), {
likeUids: newLikeUids,
likeCount: newLikeCount
})
.then (() => resolve ("DB: Uid like entry modified"))
.catch (error => reject (error))
})
}
Expected behaviour: The post is first liked, then the like status of the post gets updated, indicating a successful like. Shown behaviour: The getpostLiked function does not await the likePost execution, does not updating the like status.
Please help me find the mistake... in other instances, using the then
expression afterset
has always worked well...
Many thanks!