Skip to content
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

Fix async example to use runIfActive instead of submit #478

Merged
merged 1 commit into from Apr 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 3 additions & 10 deletions 4.0/docs/async.md
Expand Up @@ -287,23 +287,16 @@ Make sure to run any blocking work in the background. Use promises to notify the

```swift
router.get("hello") { req -> EventLoopFuture<String> in
/// Create a new void promise
let promise = req.eventLoop.makePromise(of: Void.self)

/// Dispatch some work to happen on a background thread
req.application.threadPool.submit { _ in
return req.application.threadPool.runIfActive(eventLoop: req.eventLoop) {
/// Puts the background thread to sleep
/// This will not affect any of the event loops
sleep(5)

/// When the "blocking work" has completed,
/// complete the promise and its associated future.
promise.succeed()
/// return the result.
return "Hello world!"
}

/// Wait for the future to be completed,
/// then transform the result to a simple String
return promise.futureResult.transform(to: "Hello, world!")
}
```

Expand Down