Skip to content

TypeScript: add difference between promises and async computations#9

Open
avm99963 wants to merge 1 commit intoknocte:masterfrom
avm99963-forks:push-youytoquvlpu
Open

TypeScript: add difference between promises and async computations#9
avm99963 wants to merge 1 commit intoknocte:masterfrom
avm99963-forks:push-youytoquvlpu

Conversation

@avm99963
Copy link

@avm99963 avm99963 commented Feb 4, 2026

No description provided.

* The equivalent of `await` in TypeScript, is simply the addition of the `!` character to the let statement in F#.
* In TypeScript, you can convert computation-heavy synchronous methods into asynchronous by using `async` keyword in function definition, in F# you simply wrap them with an `async{}` block (a computation expression).

Moreover, there's a subtle difference: in TypeScript, a promise can either be fulfilled or rejected. However, in F#, an `Async<'T>` object will always return a value of type `'T` once it is run. To achieve something similar in F#, you can return a value of type [`Result<'T,'TFailure>`](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/results) or use [`Async.Catch`](https://learn.microsoft.com/en-us/dotnet/fsharp/tutorials/async#asynccatch) to catch exceptions that are thrown during its execution.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @avm99963 thanks for this PR, glad to see you back over here :) I was gonna merge right away but then I double-checked how can you check if a promise has been "rejected" in TS/JS, and it's via try-catch! Which means it's kinda the same in F# actually, well, actually with F# you would start an Async job with an API call first, for example, two cases:

  • If you use Async.StartAsTask then you get a Task<T>, but if there was an a problem with the task, as soon as you try to retrieve its result, an exception would be thrown.
  • If you instead run it with Async.RunSynchronously, then the exception would be thrown right away as a result of the invocation of this Async.RunSynchronously function.

So it looks like the error handling basics of both TS/JS and F#/.NET are similar; and if you don't want to deal with exceptions but instead with error "codes" or cases, then you would use Result DU in F#, and another approach in TS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants