paulbartrum / jurassic

A .NET library to parse and execute JavaScript code.
MIT License
873 stars 122 forks source link

Promise.then() incorrectly returns the same Promise instance #170

Closed kpreisser closed 4 years ago

kpreisser commented 4 years ago

Run the following code in Jurassic:

Promise.reject(-1).then(undefined, function () { return 1; }).then(function (e) {
    console.log('resolved: ' + e);
}, function (e) {
    console.log('rejected: ' + e);
});

Actual output:

rejected: -1

Expected output: (can be seen when running in browsers)

resolved: 1

According to MDN, Promise.then returns a new Promise instance whose state will depend on the handler function.

In this case, the onRejected handler added to the first Promise returns value 1, so the Promise returned by then should be a new instance that will be resolved with the value 1.

However, it seems then as implemented in Jurassic (by #156) incorrectly returns the current Promise instance instead of creating a new one: https://github.com/paulbartrum/jurassic/blob/0438d0f98e52731e4a1e62f20d66144d59824925/Jurassic/Library/Promise/PromiseInstance.cs#L333-L355

kpreisser commented 4 years ago

//cc @jcdickinson

paulbartrum commented 4 years ago

I started looking into this, but I found a bunch more issues. I'll keep working on it.

jcdickinson commented 4 years ago

@paulbartrum let me know if you want me to pick this up.

paulbartrum commented 4 years ago

I've checked in a fix for this issue, let me know if you notice anything wrong with it.