Any reason this happens on JS ```suspend fun netwo...
# javascript
v
Any reason this happens on JS
Copy code
suspend fun networkCall(): Int {
    delay(100)
    return Random.nextInt(0,100)
}

GlobalScope.launch {
    val s1 = async { networkCall() }
    val s2 = async { networkCall() }
    val s3 = async { networkCall() }
    val s4 = async { networkCall() }
    println("${s1.await()} ${s2.await()} ${s3.await()} ${s4.await()}")
}
output is always same of one value doesn't matter what network call returns
Copy code
7 7 7 7
But this one is fine
Copy code
GlobalScope.launch {
    val s1 = async { networkCall() }
    val s2 = async { networkCall() }
    val s3 = async { networkCall() }
    val s4 = async { networkCall() }

    val s1a = s1.await()
    val s2a = s2.await()
    val s3a = s3.await()
    val s4a = s4.await()
    
    println("$s1a $s2a $s3a $s4a")
}
First one works fine on JVM
g
Looks like a bug
What is in generated JS?
a
How odd. I tried this:
Copy code
println("${s1.await()} ${s2.await()} ${s3.await()} ${s4.await()}")
            println("results: ${setOf(s1.await(), s2.await(), s3.await(), s4.await())}")
and the first line looks wrong (all same) but the second line looks right
(I’m passing in the number to return as a parameter, took the randomness out)
Copy code
println(this.result_0.toString() + ' ' + this.result_0 + ' ' + this.result_0 + ' ' + this.result_0);
this looks like the culprit
hmm, more specifically:
Copy code
case 2:
            this.state_0 = 3;
            this.result_0 = this.local$s2.await(this);
            if (this.result_0 === COROUTINE_SUSPENDED)
              return COROUTINE_SUSPENDED;
            continue;
          case 3:
            this.state_0 = 4;
            this.result_0 = this.local$s3.await(this);
            if (this.result_0 === COROUTINE_SUSPENDED)
              return COROUTINE_SUSPENDED;
            continue;
          case 4:
            this.state_0 = 5;
            this.result_0 = this.local$s4.await(this);
            if (this.result_0 === COROUTINE_SUSPENDED)
              return COROUTINE_SUSPENDED;
            continue;
          case 5:
            println(this.result_0.toString() + ' ' + this.result_0 + ' ' + this.result_0 + ' ' + this.result_0);
so as you can see, it’s reused the this.result_0 slot
definitely looks like a bug to me
i
Can you create a bug?
g
Is it IR back end?
r
I’ve seen this bug before… if you extract the async calls to their own functions, it works correctly
its got to do with how the state machine is constructed… it sometimes does not handle multiple jobs in the same parent function correctly, and slips and uses the first value for everything
a
I was testing this with 1.3.72, not IR backend