Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise implementation isn't correct #2227

Closed
mariusGundersen opened this issue Jun 23, 2022 · 1 comment
Closed

Promise implementation isn't correct #2227

mariusGundersen opened this issue Jun 23, 2022 · 1 comment

Comments

@mariusGundersen
Copy link
Contributor

While trying to implement Promise.finally I have tried to understand the implementation of promises in espruino, and it does not appear to work correctly. The chaining of promises can't work right, since promises form a DAG, not a chain. I think the promise implementation could be simplified and made to work correctly if the chain code isn't used.

Here is an example that fails in today:

function delay(ms){
  return new Promise(r => setTimeout(r, ms));
}

var p = delay(1000);

p.then(() => 'a').then(x => console.log(x)); //logs a after a second
p.then(() => 'b').then(x => console.log(x)); //should log b after a second, but logs a instead

This is probably because jswrap_promise_then calls on jswrap_promise_get_chained_promise which checks if the promise has been chained already and uses the chained promise if it has. Therefore the two thens return the same promise, which they obviously shouldn't:

var p = new Promise();

var a = p.then(() => 'a');
var b = p.then(() => 'b');

console.log(a === b); // logs true

They should both return a new unresolved promise

@mariusGundersen
Copy link
Contributor Author

This has been fixed in #2454

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant