-1

I can't use promises because it force the user to convert everything to async functions for sync methods. Is there any way to force the proxy to not to return until the callback complete?

function callbackCase() {
    function saySomething(callback) {
        callback("Stackoverflow");
    }

    console.error = new Proxy(console.error, {
        apply: (target, thisArg, args) => {
            saySomething((result) => {
                return result;
            })
        }
    })

    let test = console.error();

    console.log("From Callback Case", test);
}




function nonCallbackCase() {
    console.errors = new Proxy(console.error, {
        apply: (target, thisArg, args) => {
            return "Stackoverflow";
        }
    })

    let test = console.errors();

    console.log("From Non Callback Case", test);
}


callbackCase()
nonCallbackCase()
jeffbRTC
  • 1,941
  • 10
  • 29

1 Answers1

1

Is there any way to force the proxy to not to return until the callback complete?

No, there is not. You can't make an asynchronous operation into a synchronous operation in Javascript. Anything you try to do (like looping on a flag) will just block the event loop and thus block your asynchronous operation from completing, so you'll be deadlocked.

There are three basic ways to communicate back the results or completion of an asynchronous operation, a callback, a promise or an event. You will need to use one of those mechanisms.

Said another way, you can't squeeze an asynchronous operation into a synchronous API. You have to change the API to be an asynchronous API. Returning a promise that is tied to your asynchronous operation is the modern way to do this in Javascript. This lets the caller, then use either .then() or await to monitor completion and the result of the operation.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • what about `Promise.all` does it not would solve the problem? – Sysix Feb 07 '21 at 02:54
  • @jfriend00 What about generator functions then? I discovered them just five min ago and it seems they can be paused but one issue is that they return a object with something like {value: "result goes here"} and I need to return the result directly.. – jeffbRTC Feb 07 '21 at 02:56
  • 1
    @jeffbRTC - Generator functions don't make an asynchronous operation into a synchronous operation so I don't see how they speak to the question you asked. – jfriend00 Feb 07 '21 at 03:03
  • @jfriend00 I said you they can be paused and when async operation complete, resume then return. – jeffbRTC Feb 07 '21 at 03:04
  • @jeffbRTC - You'd have to show me how you think it applies here because I'm not seeing it. – jfriend00 Feb 07 '21 at 03:07
  • @Sysix - `Promise.all()` is just a promise-based solution for tracking multiple asynchronous promise-based operations. The OP seems to be trying to avoid returning a promise (I'm not sure why) so `Promise.all()` would not help them with that. – jfriend00 Feb 07 '21 at 03:49
  • 1
    @jeffbRTC Just like `async function`s, generator `function*`s do only pause with cooperation by the caller - they don't pause the whole thread but only the body of the function. – Bergi Feb 07 '21 at 06:21
  • @Bergi Good to have here. But what if we pause the function body and let the callback to resume the body... I am thinking some sort of loop after resumed. I'm 99% sure there is way. – jeffbRTC Feb 07 '21 at 10:15
  • A nested generator function like *function main() inside Proxy so we bypass the caller thing. The caller would be proxy itself. – jeffbRTC Feb 07 '21 at 10:15
  • There is this library called coroutines.js and it use frequency based mechsmisim to poll the result. I still need to look deep into this. – jeffbRTC Feb 07 '21 at 10:17
  • I discovered another thing. Sync XHR to service worker......Interesting... https://stackoverflow.com/a/61190354/14659574 – jeffbRTC Feb 07 '21 at 10:36
  • @jeffbRTC - Yeah, but synchronous XHR from the main thread is a bad thing. It blocks the entire browser UI. If this takes more than a second to execute, the user will think your web page is hung. Your energy here should be put into redesigning the code to use an asynchronous API, not trying to find some "hack with consequences" to work around it. I'm not going to continue to play this game. I've offered my advice. – jfriend00 Feb 07 '21 at 14:36
  • Well, you've now been researching this for at least half a day. Why don't you just learn how to program asynchronously (which will be generally useful going forward anyway) and rewrite the caller to use an asynchronous API? Just return a promise from your function and have the caller use that promise. – jfriend00 Feb 07 '21 at 17:26
  • @jfriend00 I know how to program asynchronously. It's not useful when most websites don't use "await" keyword when calling proxied functions. I inject my monkeypatch script on every website I visit. – jeffbRTC Feb 07 '21 at 20:00
  • I went with synchronous XHR anyway. – jeffbRTC Feb 07 '21 at 20:03