I have a user_dict object that is generated by fetching data from a Firebase database. Once the user_dict object is created, I store a copy of it within a profile child under the uid.
Although the necessary information is successfully saved to the database, my is_profile_ready boolean never becomes true, causing my while loop to get stuck. Is there a more effective way to handle this situation? I have struggled with promises and async/await implementation.
Could the issue lie with my while loop, or am I making another mistake that prevents is_profile_ready from turning true?
async function handleTestStart() {
traceFunction()
character.style.backgroundImage = "url(character.png)"
left_button.style.visibility = 'visible'
right_button.style.visibility = 'visible'
showLogoAnimation()
setTimeout(function () {
let start_time = new Date().getTime()
while (!is_user_dict) {
let end_time = new Date().getTime()
let time = end_time - start_time
if (time > 10000) {
timeoutError()
}
}
let is_profile_ready = createProfile()
let start_time2 = new Date().getTime()
while (!is_profile_ready) {
let end_time2 = new Date().getTime()
let time2 = end_time2 - start_time2
if (time2 > 10000) {
timeoutError()
}
}
hideLogoAnimation()
condition_category_array.forEach(function (condition_category) {
user_dict['more_info'].push(condition_category)
})
category = user_dict['more_info'][0]
backup_user_dict = deepCopyObject(user_dict)
nextQuestion()
}, 2000)
}
function createProfile() {
let current_user_id = firebase.auth().currentUser.uid
console.log('createProfile currentUser is ', current_user_id)
firebase.database().ref().child('profile').child(current_user_id).set({
'user_dict' : user_dict
}).then(() => { return true })
}
Edit: Frank suggested a solution which I'm getting an error (cannot read property "then" of undefined) on is_profile_ready.then. Copying the code below for easier reading.
async function handleTestStart() {
traceFunction()
character.style.backgroundImage = "url(character.png)"
left_button.style.visibility = 'visible'
right_button.style.visibility = 'visible'
showLogoAnimation()
let start_time = new Date().getTime()
while (!is_user_dict) {
let end_time = new Date().getTime()
let time = end_time - start_time
if (time > 10000) {
timeoutError()
}
}
let is_profile_ready = createProfile()
let start_time2 = new Date().getTime()
is_profile_ready.then((result) => {
let end_time2 = new Date().getTime()
let time2 = end_time2 - start_time2
if (time2 > 10000) {
timeoutError()
}
hideLogoAnimation()
condition_category_array.forEach(function (condition_category) {
user_dict['more_info'].push(condition_category)
})
category = user_dict['more_info'][0]
backup_user_dict = deepCopyObject(user_dict)
nextQuestion()
})
}
function createProfile() {
let current_user_id = firebase.auth().currentUser.uid
console.log('createProfile currentUser is ', current_user_id)
firebase.database().ref().child('profile').child(current_user_id).set({
'user_dict': user_dict
}).then(() => { return true })
}