8000 finally() not passing result · Issue #18 · fluffynuts/synchronous-promise · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

finally() not passing result #18

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

Closed
epernice opened this issue May 6, 2020 · 7 comments
Closed

finally() not passing result #18

epernice opened this issue May 6, 2020 · 7 comments

Comments

@epernice
Copy link
epernice commented May 6, 2020

SynchronousPromise's finally() is not returning the resolved value (tried on 2.0.10). Here's a RunKit that compares the behavior against ES6 promises.
https://runkit.com/embed/cu88waw08blm

var SynchronousPromise = require("synchronous-promise").SynchronousPromise;

let result = null;

result = await Promise.resolve("foo");
console.log("es6 promise after resolve: " + result); // foo

result = await SynchronousPromise.resolve("foo");
console.log("sync promise after resolve: " + result); // foo

result = await Promise.resolve("foo").finally();
console.log("es6 promise after finally: " + result); // foo

result = await SynchronousPromise.resolve("foo").finally();
console.log("sync promise after finally: " + result); // undefined
@epernice
Copy link
Author
epernice commented May 6, 2020

Here's how I'm working around it. Not sure whether there will be problems associated with this hack.

SynchronousPromise.prototype.finally = function(callback) {
    var ran = false;
    function runFinally() {
        if (!ran) {
            ran = true;
            return callback();
        }
    }
    return this
        .then(result => {
            runFinally();
            return result;
        })
        .catch(result => {
            runFinally();
            return result;
        });
};

Thanks, by the way, for this library! It's going to save me a LOT of work as I move a large codebase off of angular 1.x promises (with $httpBackend flushing to make tests synchronous) over to ES6 promises. I really wasn't looking forward to modifying thousands of tests to use async/await.

@fluffynuts
Copy link
Owner

@epernice resolved & published. Thanks for reporting, especially in so much detail (:

@epernice
Copy link
Author

Thanks for the quick turnaround!

The change does pass in my original Runkit, but still failed against my codebase. It looks like there is still an additional difference that crops up when a function is passed into the finally:

var SynchronousPromise = require("synchronous-promise").SynchronousPromise;


8000
let result = null;

result = await Promise.resolve("foo");
console.log("es6 promise after resolve: " + result); // foo

result = await SynchronousPromise.resolve("foo");
console.log("sync promise after resolve: " + result); // foo

result = await Promise.resolve("foo").finally(() => {});
console.log("es6 promise after finally: " + result); // foo

result = await SynchronousPromise.resolve("foo").finally(() => {});
console.log("sync promise after finally: " + result); // undefined

@fluffynuts
Copy link
Owner

Ok, please check out 2.0.12 -- I added the test above (empty finally method) and then found that there were a couple of subtle differences between how native Promises in Firefox behaved than how SynchronousPromise behaved, with respect to finally handling. Some of the behaviors don't seem logical to me -- like discarding the result from finally except if it throws, but that's what native FF promises do, so 🤷 I've implemented the same.

@fluffynuts fluffynuts reopened this May 12, 2020
@fluffynuts
Copy link
Owner

should be fixed in 0042d62

@fluffynuts
Copy link
Owner

btw, thanks for reporting -- as noted in the readme, I rely on user feedback to keep this library valuable (:

@epernice
Copy link
Author
epernice commented Aug 4, 2020

Sorry for not circling back to try this out sooner, but this solved it for me - thanks!

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

2 participants
0