Last active
October 9, 2020 17:21
-
-
Save peatiscoding/f9e6c659f6a19945dcf18c90c5d0f5f0 to your computer and use it in GitHub Desktop.
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
// | |
// 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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a slight change: take an array of promises, so callers don't have to pass an array of closures. See my gist