if (element[0] === '@notify_1_2') {
// @ts-ignore
async function checkStorage() {
const json = await AsyncStorage.getItem('@alarm')
const response = JSON.parse(json!)
let dayDifference: any
if (json !== null) {
dayDifference = daysDiff(response, Date.now())
}
if (dayDifference > 30 || dayDifference == null) {
return { id: 1, priority: 1 }
}
}
checkStorage().then((res) => {
console.log('in function on top of push', arrayTracker)
arrayTracker.push(res)
console.log('in function bottom of push', arrayTracker)
})
} else {
arrayTracker.push({ id: 0, priority: 0 })
}
}
})
console.log('array tracker:', arrayTracker)
Output:
array tracker outside function: []
in function on top of push []
in function bottom of push [{…}]
I want it so that the array tracker outside function: []
console log waits till all the async/await stuff are done with their checks.
Right now I push a value to array but it always seems empty but in the console log bottom of push it seems like the value gets passed - seems like some async/await order I couldn't get right
Output wanted:
in function on top of push []
in function bottom of push [{…}]
array tracker outside function: [{…}]. <------ with value and not empty
EDIT
response.forEach(async (element: any) => {
const formatJson = JSON.parse(element[1])
if (formatJson !== null) {
const dayDifference = daysDiff(formatJson.closedTimeStamp, Date.now())
if (dayDifference > 7) {
arrayTracker.push(formatJson)
}
} else {
if (element[0] === '@notify_1_2') {
// @ts-ignore
async function checkStorage() {
const json = await AsyncStorage.getItem('@alarm')
const response = JSON.parse(json!)
let dayDifference: any
if (json !== null) {
dayDifference = daysDiff(response, Date.now())
}
if (dayDifference > 7 || dayDifference == null) {
return { id: 1, priority: 1 }
}
}
arrayTracker.push(await checkStorage())
console.log('array tracker below push:', arrayTracker)
} else {
arrayTracker.push({ id: 0, priority: 0 })
}
}
})
console.log('array tracker outside function:', arrayTracker)
Output:
array tracker outside function: []
array tracker below push: [{…}]
Output wanted:
array tracker below push: [{…}]
array tracker outside function: [{…}]