tc39 / proposal-function-once

A TC39 proposal for an Function.prototype.once method in the JavaScript language.
BSD 3-Clause "New" or "Revised" License
46 stars 3 forks source link

Throws and rejections #7

Closed Ginden closed 2 years ago

Ginden commented 2 years ago

Let's consider following case:


function fooRaw() {
    throw new Error(`let's see stack trace');
}
const foo = fooRaw.once();

function a() {
   foo();
}

function b() {
   foo();
}

a();
b(); // What is stack trace here? Is thrown object the same?

Most of implementations don't store thrown errors, but it's important design decision.

Another thing to consider are async functions - how are rejections handled? It would be surprising for sync functions not to save call result, but save it for rejected promises.

js-choi commented 2 years ago

Thanks for raising this issue. It seems to encompass two topics: how to handle sync errors thrown by the first call and whether to handle async functions in a special way.

We’re already discussing the first topic (sync errors from the first call), in #2. In particular, see the plenary findings in https://github.com/js-choi/proposal-function-once/issues/2#issuecomment-1081942617 – the plenary was slightly in favor of returning undefined every time, as well as throwing any sync error from the first call only once—and never again on subsequent calls.

We could branch that out into this dedicated issue, I suppose. But sync error handling is closely coupled to the decision we make for #2, which is currently inclined toward returning undefined every time, so promise handling is moot anyway. (I will edit #2’s original post to mention sync error handling.)

The other topic is about async functions. If we go with once functions returning undefined every time, then we might want to also have onceAsync functions that each return the same promise from every call—otherwise it is impossible to know whether onceAsync functions have thrown errors. I think that may be out of scope of this proposal, but I can create a new issue for it anyway.

I will close this issue as a duplicate of #2 and #7. Let me know if you feel this isn’t yet resolved or adequately handled by #2 or #7.