Created
March 29, 2022 17:40
-
-
Save MaxenceMottard/e3b565782cdec9331a4f05d27cab7968 to your computer and use it in GitHub Desktop.
PromiseKit Extension to Swift Concurrency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Promise+Extension.swift | |
// blockchain-concerts | |
// | |
// Created by Maxence on 29/03/2022. | |
// | |
import Foundation | |
import PromiseKit | |
extension Promise { | |
func async() async throws -> T { | |
return try await withCheckedThrowingContinuation { continuation in | |
var result = self.result | |
if result == nil { | |
let group = DispatchGroup() | |
group.enter() | |
pipe { result = $0; group.leave() } | |
group.wait() | |
} | |
switch result! { | |
case .rejected(let error): | |
continuation.resume(throwing: error) | |
case .fulfilled(let value): | |
continuation.resume(returning: value) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment