Skip to content

Instantly share code, notes, and snippets.

@peatiscoding
Last active October 9, 2020 17:21
Show Gist options
  • Save peatiscoding/f9e6c659f6a19945dcf18c90c5d0f5f0 to your computer and use it in GitHub Desktop.
Save peatiscoding/f9e6c659f6a19945dcf18c90c5d0f5f0 to your computer and use it in GitHub Desktop.
//
// PromiseKitHelper.swift
// TrueID
//
// Created by Kittiphat Srilomsak on 3/21/2560 BE.
// Copyright © 2017 peatiscoding.me all rights reserved.
//
import PromiseKit
extension Promise {
/**
* Create a final Promise that chain all delayed promise callback all together.
*/
static func chain(_ promises:[() -> Promise<T>]) -> Promise<[T]> {
return Promise<[T]> { (fulfill, reject) in
var out = [T]()
let fp:Promise<T>? = promises.reduce(nil) { (r, o) in
return r?.then { c in
out.append(c)
return o()
} ?? o()
}
fp?.then { c -> Void in
out.append(c)
fulfill(out)
}
.catch(execute: reject)
}
}
}
@kashifshaikh
Copy link

I made a slight change: take an array of promises, so callers don't have to pass an array of closures. See my gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment