node src/infinite.mjs  SIGINT(2) ↵  1200  13:31:27
Promise { <pending> }
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
So async
...
So async represents a side effect, Much await represents the value that the promise returns. If the returned value is required it would be handled in this way:
Yeah, the problem here is that the JS interpreter knows to handle the for loop differently just because of the async keyword. It’s probably better for this case to rely on a different Promise/Future library.
The best advice i can give you is to checkout first the RxJs website and then this great ressource in order to improve your knowledge about RxJS.
Back to your usecase, you want to react to some events from some place. It is the perfect time to use an observable
So, in case you’re talking about WebSocket, you can subscribe to new messages this way (from the RxJs docs)
import { webSocket } from "rxjs/webSocket";
const subject = webSocket("ws://localhost:8081");
subject.subscribe(
msg => console.log('message received: ' + msg), // Called whenever there is a message from the server.
err => console.log(err), // Called if at any point WebSocket API signals some kind of error.
() => console.log('complete') // Called when connection is closed (for whatever reason).
);
PS: I’m working on an article about Functional Programming & Clean architecture with React / Redux Toolkit / RxJs in front-end world (in fact i don’t produce Promise anymore, i only use observable from RxJs )
Sorry, I discussed this with my colleague @cknitt who suggested that the promise chain needs to be interrupted by calling the recursive function in the setTimeout instead.
This one should work:
let n_async_event = () => {
Js.Promise.make((~resolve, ~reject as _) => {
Js.log("So async")
resolve(. "Much await")
})
}
let rec forever = () =>
n_async_event()
|> Js.Promise.then_(result => {
Js.log(result)
let _ = Js.Global.setTimeout(() => forever(), 1)
Js.Promise.resolve()
})
|> ignore
let _ = forever()