Skip to content

nfam/promise.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Promise

swift platform build codecov license

An minimal Promise Library for Swift.

Table of Contents


Install

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/nfam/promise.swift.git", majorVersion: 0)
    ]
)

API

API is similar to the standard Promise in JavaScript.

Creating a Promise


Promise((resolve,reject))

Creates a Promise with a closure that is passed two callback closure parameters to fulfill or reject the Promise. The body initiates some asynchronous work, and then, once that completes, either calls the first or second callback closure parameters to fulfill or reject the promise, respectively. If an error is thrown in the body, the Promise is rejected.

init<T>(
    in queue: DispatchQueue? = nil,
    execute body: ((T) -> Void, (Error) -> Void) -> Void
)

Promise(return)

Creates a Promise with a closure that returns a value to fulfill the Promise. The body initiates some asynchronous work, and then, once that completes, returns a value to fulfill the promise. If an error is thrown in the body, the Promise is rejected.

init<T>(
    in queue: DispatchQueue? = nil,
    execute body: () throws -> T
)

Promise(value)

Creates a Promise that is fulfilled with a given value.

init<T>(in queue: DispatchQueue? = nil, value: T)

Promise(error)

Creates a Promise that is rejected with a given error.

init(in queue: DispatchQueue? = nil, error: Error)

Type Methods


Promise.all

Returns a Promise that either fulfills when all of the promises in the iterable parameter have resolved, or rejects as soon as one of the promises in the iterable parameter rejects.

Promise.all<S>(resolved promises: S) -> Promise<[T]> where S: Sequence, S.Iterator.Element == Promise
Promise.all(_ promises: Promise<T>...) -> Promise<[T]>

Promise.race

Returns a Promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects, with the value or reason from that Promise.

Promise.race<S>(promises: S) -> Promise<T> where S: Sequence, S.Iterator.Element == Promise
Promise.race(_ promises: Promise<T>...) -> Promise<T>

Instance Methods


promise.then

Appends fulfillment and/or rejection closures to the Promise, and returns a new Promise resolving to the return value of the either executed closure.

func then<U>(
    in queue: DispatchQueue? = nil,
    fulfillment: (T) throws -> U
) -> Promise<U>

func then<U>(
    in queue: DispatchQueue? = nil,
    fulfillment: (T) throws -> Promise<U>
) -> Promise<U>

func then<U>(
    in queue: DispatchQueue? = nil,
    fulfillment: (T) throws -> U,
    rejection: (Error) throws -> U
) -> Promise<U>

func then<U>(
    in queue: DispatchQueue? = nil,
    fulfillment: (T) throws -> Promise<U>,
    rejection: (Error) throws -> Promise<U>
) -> Promise<U>

promise.catch

Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled. The Promise returned by catch() is rejected if onRejected throws an error or returns a Promise which is itself rejected; otherwise, it is resolved.

promise.catch { error in
    ...
}

promise.finally

Appends a handler to the promise, and returns a new promise which is resolved when the original promise is resolved. The handler is called when the promise is settled, whether fulfilled or rejected.

However, please note: a throw (or returning a rejected promise) in the finally callback will reject the new promise with that rejection reason.

promise.finally {
    ...
}

About

A minimal Promise library for Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages