Skip to content

Instantly share code, notes, and snippets.

@MaxenceMottard
Created March 29, 2022 17:40
Show Gist options
  • Save MaxenceMottard/e3b565782cdec9331a4f05d27cab7968 to your computer and use it in GitHub Desktop.
Save MaxenceMottard/e3b565782cdec9331a4f05d27cab7968 to your computer and use it in GitHub Desktop.
PromiseKit Extension to Swift Concurrency
//
// 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