News for hackers. Stuff that matters.
Re:Rust’s async isn’t colored by jedimastert on Thursday March 11, 2021 @07:45AM (#26423389)
I'm not usually a JS person, but you can do the same thing with Promises too, right?

Did JS Solve(tm) "colored" functions?

Re:Rust’s async isn’t colored by shakow on Thursday March 11, 2021 @08:06AM (#26423538)
> but you can do the same thing with Promises too, right?

No, Promises can only be interacted with within the async framework. There is no ways to

1. Call an async function/get a Promise in a sync function

and

2. “Extract” the value in the promise and use it as naked value.

JS would need e.g. a “stop there, wait for the promise to result, unwrap its content and give it to me” instruction to be like Rust in this case.

Re:Rust’s async isn’t colored by jedimastert on Thursday March 11, 2021 @08:11AM (#26423576)
You know what, you're right. I thought there was an "unwrap", that's my b
Re:Rust’s async isn’t colored by resonantjacket5 on Thursday March 11, 2021 @01:43PM (#26427435)
You can technically wrap the promise and spin lock on it till it resolves. Aka forcibly wait on an async function in a sync function. There are some npm libraries that provide it. I've used a couple times (for some hacky scripts), though obviously don't use it in the browser.
Re:Rust’s async isn’t colored by pitaj on Thursday March 11, 2021 @03:23PM (#26428569)
AFAIK, you can't do this from JS, you have to have some engine integration, as the busy loop in JS will prevent the event loop from working.

For instance, this will essentially halt your runtime:

    let y;
    Promise.resolve(123).then(x => { y = x; });

    while (!y) {}
    console.log(y)
Re:Rust’s async isn’t colored by lhorie on Thursday March 11, 2021 @04:08PM (#26429021)
Yes, deasync[0] for example, hooks into the Node.js event loop from c++ land

[0] https://www.npmjs.com/package/deasync

Re:Rust’s async isn’t colored by shakow on Thursday March 11, 2021 @08:13AM (#26423600)
No problem, knowledge comes from suffering ;)
Re:Rust’s async isn’t colored by mmis1000 on Thursday March 11, 2021 @01:09PM (#26427064)
No, js won't allow you to halt the whole thread to wait for your async function (most runtime implements it as single thread). But kotlin on Java does (while you have absolutely no reason to do that except workaround, you just literally get a slower sync version of that function. It is a escape hole for you to do it sync when something absolutely need it be like that (take a example, the main function))