Skip to content

Instantly share code, notes, and snippets.

let timeout = after(seconds: 4)
race(when(fulfilled: fetch(url:url)).asVoid(), timeout).then {
//…
}
func attempt<T>(maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2), _ body: @escaping () -> Promise<T>) -> Promise<T> {
var attempts = 0
func attempt() -> Promise<T> {
attempts += 1
return body().recover { error -> Promise<T> in
guard attempts < maximumRetryCount else { throw error }
return after(delayBeforeRetry).then(on: nil, attempt)
}
}
return attempt()
extension Foo {
static func promise() -> Promise<FooResult> {
return PromiseWithDelegate().promise
}
}
class PromiseWithDelegate: FooDelegate {
let (promise, seal) = Promise<FooResult>.pending()
private let foo = Foo()
login().then { username in
getTokens(for: username).map { ($0, username) }
}.then { tokens, username in
//…
}
let p1 = promise1.then {
...
}
let p2 = promise2.then {
...
}
when(fulfilled: p1, p2).catch { error in
...
func foo() -> (Promise<Void>, cancel: () -> Void) {
let task = Task(…)
var cancelme = false
let promise = Promise<Void> { seal in
task.completion = { value in
guard !cancelme else { reject(NSError.cancelledError) }
seal.fulfill(value)
}
task.start()
class ViewController: UIViewController {
private let (promise, seal) = Guarantee<...>.pending()
func show(in: UIViewController) -> Promise<…> {
in.show(self, sender: in)
return promise
}
func done() {
dismiss(animated: true)
et number = UInt32(Calendar.current.component(.weekday, from: Date()))
let msg = ["D", "(", "o", "o", "#", "(", "w", "k", "h", "#", "e", "(", "h", "v", "w", "!", "(", "#", "d", "q", "(", "g", "#", "n", "h", "h", ")", "s", "#", "r", "!", "q", "#", "h", "y", "d", "!", "o", "x", "d", "w", "(", "l", "q", "j"]
print(msg.filter{$0.unicodeScalars.map{$0.value}.reduce(0, +) > 64
    || $0.unicodeScalars.map{$0.value}.reduce(0, +) == 35}.map{
        Character(UnicodeScalar($0.unicodeScalars.map{$0.value}.reduce(0, +)
            - number)!)})
func rot3(msg: String, op: (UInt32, UInt32) -> UInt32) -> String {
let numbers = msg.unicodeScalars.map{$0.value}
return String(numbers.map{Character(UnicodeScalar(op($0, 3))!)})
}
func rotFinal(msg: String, key: String, op: (UInt32, UInt32) -> UInt32) -> String {
return String(zip(msg.unicodeScalars.map{$0.value}, String(repeating: key, count: (msg.count / key.count) + 1)
.unicodeScalars.map{$0.value})
.map{Character(UnicodeScalar(op($0.0, $0.1))!)})
}