nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Resolve promise synchronously #4411

Closed redoC-A2k closed 4 days ago

redoC-A2k commented 1 month ago

Node.js Version

20

NPM Version

10

Operating System

linux

Subsystem

async_hooks, v8, Other

Description

I want to resolve promise synchronously in nodejs anyhow . Please help me . Acutally the reason is I want to call async functions from another environment (wasm) (that environment is a trait implemented ) .

async function asyncfn (){
    let response = await axios(url);
    return response;
}

From that environment once I call any async function it returns promise . And the only way to resolve promise is to call "then" on it but then always takes callback and till that callback resolves the synchronous function in rust completes ( I can't block thread on wasm environment since it is again single threaded if I do so it gets blocked permanently) And if I do following

function fn(){
    axios(url).then(response => {
        return response.data
    }
    return "something"
}

When I call above function from wasm , then again fn function returns "something" and execution proceeds as the then callback will not execute instantaneously . It has been 7-10 days due to how difficult it is to resolve promise synchronously in nodejs . Please help me even If I need to use nodejs addon api I am fine with that but I want a way to run promise synchronously anyhow . I have used library like deasync but it did not worked , further other library like await-sync ( using worker threads ) come with limitation that async function can't access surrounding scope . If I use a library like sync-rpc (which instantiate another node process I think) then it has limitation that it needs a whole different file in cjs syntax .

Minimal Reproduction

No response

Output

No response

Before You Submit

RedYetiDev commented 1 month ago

Hi! Is this a question about WASM, or async promises?

redoC-A2k commented 1 month ago

About async promises

RedYetiDev commented 4 weeks ago

The key thing about promises is that they aren't synchronous, so I'm not understanding what your trying to achieve

RedYetiDev commented 3 weeks ago

Did that answer your question, @redoC-A2k?