Skip to content

Commit

Permalink
Fix async example to use runIfActive instead of submit (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Apr 30, 2020
1 parent 22ec917 commit b44ef1a
Showing 1 changed file with 3 additions and 10 deletions.
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

0 comments on commit b44ef1a

Please sign in to comment.