TypeScript: add difference between promises and async computations#9
Open
avm99963 wants to merge 1 commit intoknocte:masterfrom
Open
TypeScript: add difference between promises and async computations#9avm99963 wants to merge 1 commit intoknocte:masterfrom
avm99963 wants to merge 1 commit intoknocte:masterfrom
Conversation
e2d7333 to
5a7d19b
Compare
knocte
reviewed
Feb 7, 2026
| * 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. |
Owner
There was a problem hiding this comment.
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.RunSynchronouslyfunction.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.