-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[No QA] Allow async/await syntax in our codebase #70989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tgolen
merged 10 commits into
Expensify:main
from
software-mansion-labs:feat/async-await
Sep 30, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
adf75ff
Enhance contributing guides with new asynchronous code standards and …
blazejkustra 3822a22
Remove '@lwc/lwc/no-async-await' rule from various ESLint configurati…
blazejkustra 4dded45
Refactor async code guidelines in ASYNC.md to clarify rules
blazejkustra 0ad7be9
Bring back old guide about new ESNext features
blazejkustra 8fd7947
Fix formatting in ASYNC.md by adding a space after the bullet point f…
blazejkustra e50e623
Add more examples and reorganize the doc
blazejkustra 36ad537
Clarify async error handling examples in ASYNC.md
blazejkustra d19f214
Clarify async/await usage and expanding parallel execution guidelines
blazejkustra 46fff61
Merge branch 'main' of github.com:Expensify/App into feat/async-await
blazejkustra 81a8c59
fix: update electronServe to register buffer protocol with deprecatio…
blazejkustra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| ### Async vs. Sync Code | ||
|
|
||
| Async code is everywhere in our app: API calls, storage access, background tasks, test scripts, GitHub Actions, and more. This document explains how and when to use sequential vs. parallel async flows, and why our rules exist. | ||
|
|
||
| ## Why this matters | ||
|
|
||
| - Clarity: Async/await makes inherently sequential logic easier to read and review. | ||
| - Performance: Parallelizing independent work avoids unnecessary delays. | ||
| - Consistency: Shared rules make it easier for contributors inside and outside Expensify to write reliable code. | ||
|
|
||
| ## Rules | ||
|
|
||
| ### - Sequential flows SHOULD use async/await | ||
| When order matters, `async/await` expresses intent in a clear, linear style. | ||
| Example: Upload a file → Parse it → Save results. | ||
|
|
||
| ```ts | ||
| const uploaded = await uploadFile(file); | ||
| const parsed = await parseReceipt(uploaded.url); | ||
| await saveExpense(parsed); | ||
| ``` | ||
|
|
||
| ### - Independent steps MUST be run in parallel | ||
| If two operations don’t depend on each other, start them together. Here are the different ways to run them in parallel: | ||
|
|
||
| - **`Promise.all`**: All must succeed, fails fast on first rejection. Use when you need every result. | ||
|
|
||
| ```ts | ||
| const [user, permissions] = await Promise.all([ | ||
| getUser(), | ||
| getPermissions(), | ||
| ]); | ||
| ``` | ||
|
|
||
| - **`Promise.allSettled`**: Wait for everything, regardless of failures. Use when you don't need all results. | ||
|
|
||
| ```ts | ||
| const results = await Promise.allSettled([syncReceipts(), syncInvoices(), syncReports()]); | ||
| const succeeded = results.filter(r => r.status === 'fulfilled'); | ||
| const failed = results.filter(r => r.status === 'rejected'); | ||
| ``` | ||
|
|
||
| - **`Promise.any`**: Return the first successful result, ignore failures until one resolves. Great for redundant sources. | ||
|
|
||
| ```ts | ||
| const fastConfig = await Promise.any([ | ||
| fetchFromCDN(), | ||
| fetchFromBackup(), | ||
| fetchFromLocalMirror(), | ||
| ]); | ||
| ``` | ||
|
|
||
| ### - UI SHOULD launch independent async calls in parallel | ||
| Components should not wait for one API call before starting another unless there is a dependency. Rendering must never be blocked by network requests. | ||
|
|
||
| Refer to [DATA-BINDING.md](./DATA-BINDING.md) for full details. | ||
|
|
||
| ### - Sequential logic SHOULD be encapsulated outside the UI | ||
| If a flow really must happen in order, write it in `src/libs/` or an action/helper. The UI should call that as a single logical operation. | ||
|
|
||
|
|
||
| ### - `async/await `SHOULD be preferred over `.then/.catch` | ||
| Use `async/await` unless you’re: | ||
|
|
||
| * Wrapping callback-based APIs. | ||
|
|
||
| ```ts | ||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => { | ||
| setTimeout(resolve, ms); | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| * Creating deferred promises (signals like “ready” or “loaded”). | ||
|
|
||
| ```ts | ||
| let resolveReady: () => void; | ||
|
|
||
| const isReady = new Promise<void>((resolve) => { | ||
| resolveReady = resolve; | ||
| }); | ||
|
|
||
| // later in the code | ||
| resolveReady(); | ||
| ``` | ||
|
|
||
| ## More Examples | ||
|
|
||
| ### 1) Error handling SHOULD use `.catch()` | ||
|
|
||
| Error handling style depends on context. If you’re handling a single async operation, `.catch()` is concise and effective. | ||
|
|
||
| ```ts | ||
| // PREFERRED | ||
| async function getData(url: string) { | ||
| const data = await fetch(url).catch(() => fetchFallback(url)); | ||
| return process(data); | ||
| } | ||
| ``` | ||
|
|
||
| ```ts | ||
| // BAD — needs an outer let just to span try/catch, adds noise and requires a mutable variable | ||
| async function getData(url: string) { | ||
| let data: DataType | undefined; | ||
| try{ | ||
| data = await fetch(url) | ||
| } catch (e){ | ||
| data = fetchFallback(url) | ||
| } | ||
| return process(data); | ||
| } | ||
| ``` | ||
|
|
||
| If you need to handle multiple sequential async operations, `try/catch` provides cleaner flow and better readability. | ||
|
|
||
| ```ts | ||
| async function getData(url: string) { | ||
| try { | ||
| const response = await fetch(url); | ||
| const data = await response.json(); | ||
| return process(data); | ||
| } catch (error) { | ||
| const data = fetchFallback(url); | ||
| return process(data); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### 2) Passing promises to `use` and other helpers | ||
|
|
||
| ```ts | ||
| // An async function returns a promise and can be passed to helpers expecting a promise | ||
| const dataPromise = useMemo(() => loadDashboard(), []); | ||
| use(dataPromise); | ||
| ``` | ||
|
blazejkustra marked this conversation as resolved.
|
||
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.