Reputation: 7642
I am trying to write a simple function like this
const joinLeague = async () => {
const league = await attemptToJoinLeaugeIfItExists({ id, leaguePin })
console.log(league, 'fl')
if (league) {
// do something
}
}
and I have made a firebase helper like this
export const attemptToJoinLeaugeIfItExists = ({ id, leaguePin }: any) => {
return new Promise((res, rej) => {
res(
firebaseApp
.database()
.ref(`users/${id}`)
.once('value')
.then((snapshot) => { `
firebaseApp
.database()
.ref(`leagues`)
.once('value')
.then((snapshot) => {
console.log('in here')
})
}),
)
})
}
however, league
is logging out as undefined and in here
is logging out second. however, I thought it would be the other way around? I thought it would "await" for this function to resolve then once it's resolved, it would show me the result. what am I doing wrong?
Upvotes: 3
Views: 86
Reputation: 29325
do it like this:
export const attemptToJoinLeaugeIfItExists = ({ id, leaguePin }: any) => {
return firebaseApp
.database()
.ref(`users/${id}`)
.once('value')
.then((snapshot) => { `
return firebaseApp
.database()
.ref(`leagues`)
.once('value')
})
}
you're currently immediately resolving your outer promise but with an inner promise, and that inner promise doesn't return anything at all, hence undefined. Just return the promise directly. Cleaner, simpler.
Upvotes: 1