The title is confusing. Here's the problem I'm trying to solve
I have a sorted array of numbers. The numbers represent unix timestamps
let timeIndex = [2, 6, 10, 67, 300, 800000]
I want to put a new time in that array
let addTime = (time: number) => {
let index = getIndex(timeIndex, time)
timeIdx.splice(index, 0, time)
}
The thing is though, I'm running ^this function^ inside of a callback, and that callback doesnt wait until the previous one is finished
database.listenFor('posts').whenFound(post => {
addTime(post.time)
})
This will cause errors, because the function addTime will run parallel with itself, and that'll screw with the indexes
Assume there's no way to change the behaviour of whenFound, and waiting for all the posts isnt an option. how do I make addTime run syncronously?