- From: Ferdinand Prantl <notifications@github.com>
- Date: Sat, 13 Mar 2021 01:05:13 -0800
- To: WICG/webcomponents <webcomponents@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Saturday, 13 March 2021 09:05:29 UTC
@Rich-Harris , I wonder why you did not use `push`, when you knew that you would fill the array asynchronously. You showed a race condition, but it was caused by a bug in the code. I can imagine other bugs caused by assigning into a variable.
Was you intention to prevent such bugs in the client code by a more limited interface? Did you have other intention?
Corrected code:
```js
arr = ['a'];
function resolveAfter (value, ms) {
return new Promise(f => setTimeout(() => f(value), ms));
}
async function addB() {
arr.push(await resolveAfter('b', 100));
}
async function addC() {
arr.push(await resolveAfter('c', 50));
}
addB();
addC();
// later...
console.log(arr); // ['a', 'c', 'b']
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/759#issuecomment-797988621
Received on Saturday, 13 March 2021 09:05:29 UTC