The current method to solve this problem in rescript for me is blow, just too complex :
let promise = ref(Promise.resolve(true))
for i in 0 to 9 {
promise.contents = promise.contents->Promise.then(status => {
let p = if (status) {
sleep(10)
} else {
Promise.resolve()
}
p->Promise.then(_ => {
if (i >= 5) {
// simulate break, this is the only way to handle dynamic break condition for me right now
Promise.resolve(false)
} else {
Promise.resolve(true)
}
})
})
}
Just to throw my hat in the ring, here’s an implementation without reduce or recursion, all three solutions have pros and cons. I would probably opt for either this or the recursive method.
let start = () => {
let promise = ref(Promise.resolve())
for _ in 1 to 5 {
promise.contents =
promise.contents->Promise.then(_ => sleep(500)->Promise.thenResolve(_ => Js.log("yo")))
}
promise
}