Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions context/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,42 @@ end
puts "The number was: #{task.wait}"
```

### Comparing `Async` and `Sync`

If you are familiar with JavaScript's `async`/`await`, `Async{...}` is similar to calling an asynchronous function: it starts the work and returns a promise-like {ruby Async::Task} that you can wait on later.

```ruby
task = Async do
bar
end

task.wait # Returns the value of bar.
```

This is similar to JavaScript code that keeps the promise:

```javascript
const promise = bar();

await promise; // Returns the value of bar.
```

`Sync{...}` is similar to immediately awaiting that work: it runs the block in an event loop and returns the block's value directly.

```ruby
result = Sync do
bar
end
```

This is similar to:

```javascript
const result = await bar();
```

The main difference is that JavaScript starts with an event loop already available, while Ruby code needs one to be provided by a fiber scheduler. `Sync{...}` is the usual way to create or reuse that event loop when the caller wants a direct return value instead of a task.

## Creating a Fiber Scheduler

The first (top level) async block will also create an instance of {ruby Async::Reactor} which is a subclass of {ruby Async::Scheduler} to handle the event loop. You can also do this directly using {ruby Fiber.set_scheduler}:
Expand Down
36 changes: 36 additions & 0 deletions guides/getting-started/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,42 @@ end
puts "The number was: #{task.wait}"
```

### Comparing `Async` and `Sync`

If you are familiar with JavaScript's `async`/`await`, `Async{...}` is similar to calling an asynchronous function: it starts the work and returns a promise-like {ruby Async::Task} that you can wait on later.

```ruby
task = Async do
bar
end

task.wait # Returns the value of bar.
```

This is similar to JavaScript code that keeps the promise:

```javascript
const promise = bar();

await promise; // Returns the value of bar.
```

`Sync{...}` is similar to immediately awaiting that work: it runs the block in an event loop and returns the block's value directly.

```ruby
result = Sync do
bar
end
```

This is similar to:

```javascript
const result = await bar();
```

The main difference is that JavaScript starts with an event loop already available, while Ruby code needs one to be provided by a fiber scheduler. `Sync{...}` is the usual way to create or reuse that event loop when the caller wants a direct return value instead of a task.

## Creating a Fiber Scheduler

The first (top level) async block will also create an instance of {ruby Async::Reactor} which is a subclass of {ruby Async::Scheduler} to handle the event loop. You can also do this directly using {ruby Fiber.set_scheduler}:
Expand Down
Loading