I have the following code appearing in many of my JavaScript files:
import { doA, doB, doC } from 'asyncDoer'
// ... other stuff
let isDoneA = false
let isDoneB = false
let isDoneC = false
doA.then(_ => isDoneA = true)
doB.then(_ => isDoneB = true)
doC.then(_ => isDoneB = true)
// ... other stuff
I want to implement Don't Repeat Yourself and put that block into a single exported function in asyncDoer. Something like doAll().
But no matter how much I think about it, I can't figure it out. Mainly because they are 2+ unrelated promises that resolve independently. One idea I had was to somehow pass isDoneA,B,C as references so the function modifies them on promise resolution. But I heard that modifing the source parameters is a bad idea that makes the code harder to read and mantain.
Can anyone help me in how you would make that block into a more concise, repeatable unit?
Seems you need
Promise.all():Another variant:
Usage:
It's not clear though how do you use
isDonesince there's no reactivity/callbacks. A callback version would like this:Usage:
Regarding myself I often use async generators for stuff like this. For a generator you need rather
Promise.race():