Skip to content

Commit

Permalink
Rename await to awaitPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
nsoojin committed May 4, 2021
1 parent afa9a1a commit 5d07081
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -38,7 +38,7 @@ Objective-C and Swift to facilitate writing asynchronous code.
* [All](g3doc/index.md#all)
* [Always](g3doc/index.md#always)
* [Any](g3doc/index.md#any)
* [Await](g3doc/index.md#await)
* [AwaitPromise](g3doc/index.md#awaitpromise)
* [Delay](g3doc/index.md#delay)
* [Race](g3doc/index.md#race)
* [Recover](g3doc/index.md#recover)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Promises/Promise+Await.swift
Expand Up @@ -19,7 +19,7 @@ import FBLPromises
/// - promise: Promise to wait for.
/// - throws: Error the promise was rejected with.
/// - returns: Value the promise was fulfilled with.
public func await<Value>(_ promise: Promise<Value>) throws -> Value {
public func awaitPromise<Value>(_ promise: Promise<Value>) throws -> Value {
var outError: NSError?
let outValue = __FBLPromiseAwait(promise.objCPromise, &outError) as AnyObject
if let error = outError { throw error }
Expand Down
14 changes: 7 additions & 7 deletions Tests/PromisesTests/Promise+AwaitTests.swift
Expand Up @@ -20,17 +20,17 @@ class PromiseAwaitTests: XCTestCase {
func testPromiseAwaitFulfill() {
// Act.
let promise = Promise<Int>(on: .global()) { () -> Int in
let minusFive = try await(Harness.negate(5))
let minusFive = try awaitPromise(Harness.negate(5))
XCTAssertEqual(minusFive, -5)
let twentyFive = try await(Harness.multiply(minusFive, minusFive))
let twentyFive = try awaitPromise(Harness.multiply(minusFive, minusFive))
XCTAssertEqual(twentyFive, 25)
let twenty = try await(Harness.add(twentyFive, minusFive))
let twenty = try awaitPromise(Harness.add(twentyFive, minusFive))
XCTAssertEqual(twenty, 20)
let five = try await(Harness.subtract(twentyFive, twenty))
let five = try awaitPromise(Harness.subtract(twentyFive, twenty))
XCTAssertEqual(five, 5)
let zero = try await(Harness.add(minusFive, five))
let zero = try awaitPromise(Harness.add(minusFive, five))
XCTAssertEqual(zero, 0)
return try await(Harness.multiply(zero, five))
return try awaitPromise(Harness.multiply(zero, five))
}

// Assert.
Expand All @@ -42,7 +42,7 @@ class PromiseAwaitTests: XCTestCase {
func testPromiseAwaitReject() {
// Arrange & Act.
let promise = Promise<Int>(on: .global()) {
return try await(Harness.fail(Test.Error.code42))
return try awaitPromise(Harness.fail(Test.Error.code42))
}

// Assert.
Expand Down
22 changes: 11 additions & 11 deletions g3doc/index.md
Expand Up @@ -1212,9 +1212,9 @@ any( any(p1, p2), any(p3, p4)).then { results in
}
```

### Await
### AwaitPromise

Using `await` you can synchronously wait for a promise to get resolved
Using `awaitPromise` you can synchronously wait for a promise to get resolved
on a different thread. That can be useful for situations when you need
to mix several results from multiple async routines differently, i.e.
cannot chain them in a clear pipeline using [`then`](#then),
Expand All @@ -1224,12 +1224,12 @@ Swift:

```swift
Promise<Int> {
let minusFive = try await(calculator.negate(5))
let twentyFive = try await(calculator.multiply(minusFive, minusFive))
let twenty = try await(calculator.add(twentyFive, minusFive))
let five = try await(calculator.subtract(twentyFive, twenty))
let zero = try await(calculator.add(minusFive, five))
return try await(calculator.multiply(zero, five))
let minusFive = try awaitPromise(calculator.negate(5))
let twentyFive = try awaitPromise(calculator.multiply(minusFive, minusFive))
let twenty = try awaitPromise(calculator.add(twentyFive, minusFive))
let five = try awaitPromise(calculator.subtract(twentyFive, twenty))
let zero = try awaitPromise(calculator.add(minusFive, five))
return try awaitPromise(calculator.multiply(zero, five))
}.then { result in
// ...
}.catch { error in
Expand Down Expand Up @@ -1265,14 +1265,14 @@ Objective-C
Note: In the above examples it's assumed that all calculator routines are
executed asynchronously on a background thread, because the promise work block
is dispatched on a [default queue](#default-dispatch-queue) since no other is
specified, and so any blocking `await` would cause a deadlock if it waited for
specified, and so any blocking `awaitPromise` would cause a deadlock if it waited for
a promise that was going to be resolved on the default queue as well. Generally,
it's usually safer to use `await` from a global concurrent queue only to avoid
it's usually safer to use `awaitPromise` from a global concurrent queue only to avoid
any potential deadlocks. Like so:
```swift
Promise<Int>(on: .global()) {
try await(object.someAsyncRoutine())
try awaitPromise(object.someAsyncRoutine())
}
```

Expand Down

0 comments on commit 5d07081

Please sign in to comment.