Skip to content

Commit

Permalink
✏️ rewrite alternatives → scheduler.postTask() section
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Mar 18, 2024
1 parent bd513a7 commit ad399c9
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,7 @@ Web Workers are a great fit if you have: 1) heavy algorithm (e.g. image processi

### `scheduler.postTask()`

[`scheduler.postTask()`](https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask) is available in some browsers today. `postTask` is a great alternative, you just need to have a better understanding on its inner workings. `main-thread-scheduling` aims to be easier to use. For example, `main-thread-scheduling` uses the `isInputPending()` API to ensure the UI doesn't freeze when the user interacts with the page (if you use `scheduler.postTask()` you will need to do that manually). Also, if you have running animations while running your tasks, you will see `main-thread-scheduling` perform better.

If you want the benefits of `main-thread-scheduling`, but you prefer the callback based `postTask()` API/thinking model, then here is an implementation of `postTask()` using `yieldOrContinue()`:
```ts
async function postTask(callback: () => void | Promise<void>) {
await yieldOrContinue('interactive')
await callback()
}
```
[`scheduler.postTask()`](https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask) is available in some browsers today. `postTask()` and `main-thread-scheduling` share some commonalities. You can think of `postTask()` as a lower level API — it might be the right choice in specific scenarios. Library owners might be interested in exploring the nuanced differences between the two. For most cases, `main-thread-scheduling` provides a `scheduleTask()` method that mimics that API of `postTask()` while providing the extra benefits of the library.

<!--
Expand Down

0 comments on commit ad399c9

Please sign in to comment.