last day (15 days later) » 

00:53
-5
Q: How to detect whether function return value discarded or not?

jeffbRTCUse-case: This allows to differ whether the user have used promise-based style or callback style thus I can avoid double computations. I monkey-patch both function and that on-completed setter with ES6 Proxy and now I'm doing expensive computations in both. I want to avoid that. There's no way kn...

I believe JS can do this too. I don't know how , but I believe JS itself CAN'T do this.
There is no way to do this. Perhaps you could explain "why" you need this capability and we can help with an overall solution.
@Mhmdrz_A I said that because JS felt more liberal and of course it has impressive runtime polymorphism.
@Mhmdrz_A If you look at my error object idea, it does what I want to some extent. I was looking if there are alternative way on JS that I'm not aware of
there might some sort of workarounds using Proxy objects, but you Didn't explain what you wanted to achieve, or how it would help you to determine whether it is returned or not
@Mhmdrz_A I'm actually using Proxies. if function return value saved, I need to return a promise else I need to pass the result to callback (The callback not passed to function instead it saved to on- type event property on an object)
00:53
As far as I know, JavaScript doesn’t expose this kind of control flow information. You could analyze this with a tool like ESTree or some other static analysis tool, but you can’t reflect this behavior from the executing code itself.
@SebastianSimon It is not exposed, but I firmly believe that it can be retrieved passively. My error object attempt demonstrate my point.
@ScottMarcus Have a look at my new update. It's possible ..
its really a compile time thing. consider using typescript
@DanielA.White I want it at Runtime because I'm dealing with a Runtime problem. I'd not post this question If this can be solved at compile-time.
sounds like an x-y problem: why do you need it at runtime? help us understand the underlying problem.
@DanielA.White Please see my 6th comment.
00:53
@jeffbRTC please make it clear in the question itself
@DanielA.White Suggest an edit.
optimizing whether or not the return value is used or not breaks the concept that a function should only do one thing well and shouldn't care about its callers. doing special calculations to figure out the proper call site and to determine whether or not the value is used can be super error prone and costly.
add a parameter as flag
@DanielA.White You're talking about Userland functions. I'm talking about monkey patching a native function. The parsing cost still small compared to the operation I'm performing which often can take minutes.
@Juraj Adding flag is not solution when I'm monkey patching a native function. Sorry, I'm not going to explain my self any further!
@jeffbRTC Why can't you do this check on the call site?
00:53
I don't think it can be done without modifying the runtime. Parsing the call site code might work if you want to produce quick warnings, but it will not work in all cases (line breaks, inaccessible code) and you have other cases that you didn't think of: arguments, returns and variables. The result might eventually get unused, even though locally it's moved around. I don't see why the promise-based model cannot be followed by default, and nativeFooOnCompleted be called at the end.
I also don't think parsing the original code is really a "runtime" solution – the real solution is to decide based on whether the returned object is accessed or collected (but neither of those two actions may happen during the lifetime of the program).
I’d suggest you to attempt this using static analysis at authoring time (i.e. when the code is being written). You mention in your meta question, that you need this for optimization but all your attempts will negatively impact this a lot more. And your approach so far skips valid use cases (e.g. passing the return value to another function) and is also very easy to circumvent if you want to (e.g. myfunc() // =). This simply cannot be done at runtime from within JavaScript.
@poke I can't use Static because I'm writing a privacy tool which monkey patch certain native functions on the fly.
@IS4 The problem is that websites follows different ways. Some use promise and some don't. It's not something under my control.
@poke Well, I commented on the code that I didn't handle advanced cases like minified ones or the ones you state. I can handle them with an AST.
"by the time website call promise based version, the on-completed setter is empty" – wouldn't just scheduling a microtask help? MWE: jsbin.com/gozagusaci/edit?html,js,output
@TheVee Interesting idea but your code doesn't seems run at all.
@TheVee But the websites will not call "queueMicrotask" function at all.
@jeffbRTC Sorry, jsBin saved some historical draft version!!! I'll try to recompose what I had. The point is exactly that the caller does not have to do anything special at all.
@jeffbRTC It will be safer here. jsfiddle.net/7hgbfawy
The point being that using await schedules the rest of the function as a microtask. Please note that I don't attempt in any way to detect whether a value has been discarded or not. This is solely in answer to the first paragraph (Use-case), dropping the need for the code analysis. Slightly more comment here → jsfiddle.net/oztcb40j
00:53
@TheVee This looks like a black magic to me. How come it doesn't await forever since the variable is undefined? Does that micro-tasks do something magical?
@jeffbRTC await nonPromiseValue is the same as await Promise.resolve(nonPromiseValue). It completes "instantly" but still schedules the code after the await expression to run later. So with f = async () => { await 1; 2;} and calling f(); g() the code will first reach await 1 -> sleep f and schedule the rest on the microtask queue -> call g() -> (eventually when the microtask queue gets to it) resume f() continuing with 2
I should have said that it's not essential that the thing you're awaiting is onCompleted, it can be anything at all (like the number 1 above). The purpose is just to yield control to the calling routine. But I thought await onCompleted kind of explains the purpose of the line. The values from which to what it changes, or whether it does at all, do not make difference.
@TheVee Question is reopened, you can post the answer

last day (15 days later) »