Rename await to awaitPromise

This commit is contained in:
Soojin Ro committed 2021-05-04 10:34:13 +08:00
1 parent afa9a1ace7
commit 5d070816f1
4 files changed
+20 -20

No files matched your search

+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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 }
+7 -7
View File
@@ -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.
@@ -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.
+11 -11
View File
@@ -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),
@@ -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
@@ -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())
}
```