google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 294 forks source link

Waiting for resolving Promise.all #115

Closed Tchoupinax closed 5 years ago

Tchoupinax commented 5 years ago

Hello,

After making all method working well, i try to await this to resolve before to continue the next code. This following is my code

all(a, b, c)
  .then { aResult, bResult, cResult, in
    StorageService.setItem(key: "a", value: a.rawString()!)
    StorageService.setItem(key: "b", value: b.rawString()!)
    StorageService.setItem(key: "c", value: c.rawString()!)
    print("COMPLETION")
  }
  .catch { error in print(error) }
print("END")

a, b and c are three promises with each one a specific DispatchQueue. I tried many things, as the following

Promise<Int>(on: .global()) {
  try await(all(a, b, c)
  .then { aResult, bResult, cResult, in
    StorageService.setItem(key: "a", value: a.rawString()!)
    StorageService.setItem(key: "b", value: b.rawString()!)
    StorageService.setItem(key: "c", value: c.rawString()!)
    print("COMPLETION")
  }
  .catch { error in print(error) }
print("END"))
}

The problem is that if i replace the thread global by .mainor other, i make a deadlock promise. With .global()i do not have the problem but code execution does not wait for the resolve. I am wondering how can i make it, in the best way as far as possible. :)

Thank you for your time. :)

shoumikhin commented 5 years ago

Hi @Tchoupinax,

How about the following:

let storageQueue = DispatchQueue(label: "StorageQueue", qos: .userInitiated)

all(a, b, c).then(on: storageQueue) { aResult, bResult, cResult, in
  StorageService.setItem(key: "a", value: a.rawString()!)
  StorageService.setItem(key: "b", value: b.rawString()!)
  StorageService.setItem(key: "c", value: c.rawString()!)
}.then {
 print("COMPLETION")
}.catch {
 error in print(error)
}