⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Overview

Hydra

Lightweight full-featured Promises, Async & Await Library in Swift

What's this?

Hydra is full-featured lightweight library which allows you to write better async code in Swift 3.x/4.x. It's partially based on JavaScript A+ specs and also implements modern construct like await (as seen in Async/Await specification in ES8 (ECMAScript 2017) or C#) which allows you to write async code in sync manner. Hydra supports all sexiest operators like always, validate, timeout, retry, all, any, pass, recover, map, zip, defer and retry.
Starts writing better async code with Hydra!

A more detailed look at how Hydra works can be found in ARCHITECTURE file or on Medium.

❤️ Your Support

Hi fellow developer!
You know, maintaing and developing tools consumes resources and time. While I enjoy making them your support is foundamental to allow me continue its development.

If you are using SwiftLocation or any other of my creations please consider the following options:

Introduction

What's a Promise?

A Promise is a way to represent a value that will exists, or will fail with an error, at some point in the future. You can think about it as a Swift's Optional: it may or may not be a value. A more detailed article which explain how Hydra was implemented can be found here.

Each Promise is strong-typed: this mean you create it with the value's type you are expecting for and you will be sure to receive it when Promise will be resolved (the exact term is fulfilled).

A Promise is, in fact, a proxy object; due to the fact the system knows what success value look like, composing asynchronous operation is a trivial task; with Hydra you can:

  • create a chain of dependent async operation with a single completion task and a single error handler.
  • resolve many independent async operations simultaneously and get all values at the end
  • retry or recover failed async operations
  • write async code as you may write standard sync code
  • resolve dependent async operations by passing the result of each value to the next operation, then get the final result
  • avoid callbacks, pyramid of dooms and make your code cleaner!

Updating to >=0.9.7

Since 0.9.7 Hydra implements Cancellable Promises. In order to support this new feature we have slightly modified the Body signature of the Promise; in order to make your source code compatible you just need to add the third parameter along with resolve,reject: operation. operation encapsulate the logic to support Invalidation Token. It's just and object of type PromiseStatus you can query to see if a Promise is marked to be cancelled from the outside. If you are not interested in using it in your Promise declaration just mark it as _.

To sum up your code:

return Promise<Int>(in: .main, token: token, { resolve, reject in ...

needs to be:

return Promise<Int>(in: .main, token: token, { resolve, reject, operation in // or resolve, reject, _

Create a Promise

Creating a Promise is trivial; you need to specify the context (a GCD Queue) in which your async operations will be executed in and add your own async code as body of the Promise.

This is a simple async image downloader:

func getImage(url: String) -> Promise<UIImage> {
    return Promise<UIImage>(in: .background, { resolve, reject, _ in
        self.dataTask(with: request, completionHandler: { data, response, error in
            if let error = error {
                reject(error)
            } else if let data = data, let response = response as? HTTPURLResponse {
                resolve((data, response))
            } else {
                reject("Image cannot be decoded")
            }
        }).resume()
    })
}

You need to remember only few things:

How to use a Promise

Using a Promise is even easier.
You can get the result of a promise by using then function; it will be called automatically when your Promise fullfill with expected value. So:

getImage(url).then(.main, { image in
	myImageView.image = image
})

As you can see even then may specify a context (by default - if not specified - is the main thread): this represent the GCD queue in which the code of the then's block will be executed (in our case we want to update an UI control so we will need to execute it in .main thread).

But what happened if your Promise fail due to a network error or if the image is not decodable? catch func allows you to handle Promise's errors (with multiple promises you may also have a single errors entry point and reduce the complexity).

getImage(url).then(.main, { image in
	myImageView.image = image
}).catch(.main, { error in
	print("Something bad occurred: \(error)")
})

Chaining Multiple Promises

Chaining Promises is the next step thought mastering Hydra. Suppose you have defined some Promises:

func loginUser(_ name:String, _ pwd: String)->Promise<User>
func getFollowers(user: User)->Promise<[Follower]>
func unfollow(followers: [Follower])->Promise<Int>

Each promise need to use the fulfilled value of the previous; plus an error in one of these should interrupt the entire chain.
Doing it with Hydra is pretty straightforward:

loginUser(username,pass).then(getFollowers).then(unfollow).then { count in
	print("Unfollowed \(count) users")
}.catch { err in
	// Something bad occurred during these calls
}

Easy uh? (Please note: in this example context is not specified so the default .main is used instead).

Cancellable Promises

Cancellable Promises are a very sensitive task; by default Promises are not cancellable. Hydra allows you to cancel a promise from the outside by implementing the InvalidationToken. InvalidationToken is a concrete open class which is conform to the InvalidatableProtocol protocol. It must implement at least one Bool property called isCancelled.

When isCancelled is set to true it means someone outside the promise want to cancel the task.

Its your responsibility to check from inside the Promise's body the status of this variable by asking to operation.isCancelled. If true you can do all your best to cancel the operation; at the end of your operations just call cancel() and stop the workflow.

Your promise must be also initialized using this token instance.

This is a concrete example with UITableViewCell: working with table cells, often the result of a promise needs to be ignored. To do this, each cell can hold on to an InvalidationToken. An InvalidationToken is an execution context that can be invalidated. If the context is invalidated, then the block that is passed to it will be discarded and not executed.

To use this with table cells, the queue should be invalidated and reset on prepareForReuse().

class SomeTableViewCell: UITableViewCell {
    var token = InvalidationToken()

	func setImage(atURL url: URL) {
		downloadImage(url).then(in: .main, { image in
			self.imageView.image = image
		})
	}

	override func prepareForReuse() {
		super.prepareForReuse()
		token.invalidate() // stop current task and ignore result
		token = InvalidationToken() // new token
	}

	func downloadImage(url: URL) -> Promise<UIImage> {
		return Promise<Something>(in: .background, token: token, { (resolve, reject, operation) in
		// ... your async operation

		// somewhere in your Promise's body, for example in download progression
		// you should check for the status of the operation.
		if operation.isCancelled {
			// operation should be cancelled
			// do your best to cancel the promise's task
			operation.cancel() // request to mark the Promise as cancelled
			return // stop the workflow! it's important
		}
		// ... your async operation
		})
	}
}

Await & Async: async code in sync manner

Have you ever dream to write asynchronous code like its synchronous counterpart? Hydra was heavily inspired by Async/Await specification in ES8 (ECMAScript 2017) which provides a powerful way to write async doe in a sequential manner.

Using async and await is pretty simple.

NOTE: Since Hydra 2.0.6 the await function is available under Hydra.await() function in order to supress the Xcode 12.5+ warning (await will become a Swift standard function soon!)

For example the code above can be rewritten directly as:

// With `async` we have just defined a Promise which will be executed in a given
// context (if omitted `background` thread is used) and return an Int value.
let asyncFunc = async({ _ -> Int in // you must specify the return of the Promise, here an Int
	// With `await` the async code is resolved in a sync manner
	let loggedUser = try Hydra.await(loginUser(username,pass))
	// one promise...
	let followersList = try Hydra.await(getFollowers(loggedUser))
	// after another...
	let countUnfollowed = try Hydra.await(unfollow(followersList))
	// ... linearly
	// Then our async promise will be resolved with the end value
	return countUnfollowed
}).then({ value in // ... and, like a promise, the value is returned
	print("Unfollowed \(value) users")
})

Like magic! Your code will run in .background thread and you will get the result of each call only when it will be fulfilled. Async code in sync sauce!

Important Note: await is a blocking/synchronous function implemented using semaphore. Therefore, it should never be called in main thread; this is the reason we have used async to encapsulate it. Doing it in main thread will also block the UI.

async func can be used in two different options:

  • it can create and return a promise (as you have seen above)
  • it can be used to simply execute a block of code (as you will see below)

As we said we can also use async with your own block (without using promises); async accepts the context (a GCD queue) and optionally a start delay interval. Below an example of the async function which will be executed without delay in background:

async({
	print("And now some intensive task...")
	let result = try! Hydra.await(.background, { resolve,reject, _ in
		delay(10, context: .background, closure: { // jut a trick for our example
			resolve(5)
		})
	})
	print("The result is \(result)")
})

There is also an await operator:

  • await with throw: .. followed by a Promise instance: this operator must be prefixed by try and should use do/catch statement in order to handle rejection of the Promise.
  • await without throw: ..! followed by a Promise instance: this operator does not throw exceptions; in case of promise's rejection result is nil instead.

Examples:

async({
	// AWAIT OPERATOR WITH DO/CATCH: `..`
	do {
		let result_1 = try ..asyncOperation1()
		let result_2 = try ..asyncOperation2(result_1) // result_1 is always valid
	} catch {
		// something goes bad with one of these async operations
	}
})

// AWAIT OPERATOR WITH NIL-RESULT: `..!`
async({
	let result_1 = ..!asyncOperation1() // may return nil if promise fail. does not throw!
	let result_2 = ..!asyncOperation2(result_1) // you must handle nil case manually
})

When you use these methods and you are doing asynchronous, be careful to do nothing in the main thread, otherwise you risk to enter in a deadlock situation.

The last example show how to use cancellable async:

func test_invalidationTokenWithAsyncOperator() {

// create an invalidation token
let invalidator: InvalidationToken = InvalidationToken()

async(token: invalidator, { status -> String in
	Thread.sleep(forTimeInterval: 2.0)
	if status.isCancelled {
		print("Promise cancelled")
	} else {
		print("Promise resolved")
	}
	return "" // read result
}).then { _ in
	// read result
}

// Anytime you can send a cancel message to invalidate the promise
invalidator.invalidate()
}

Await an zip operator to resolve all promises

Await can be also used in conjuction with zip to resolve all promises from a list:

let (resultA,resultB) = Hydra.await(zip(promiseA,promiseB))
print(resultA)
print(resultB)

All Features

Because promises formalize how success and failure blocks look, it's possible to build behaviors on top of them. Hydra supports:

  • always: allows you to specify a block which will be always executed both for fulfill and reject of the Promise
  • validate: allows you to specify a predica block; if predicate return false the Promise fails.
  • timeout: add a timeout timer to the Promise; if it does not fulfill or reject after given interval it will be marked as rejected.
  • all: create a Promise that resolved when the list of passed Promises resolves (promises are resolved in parallel). Promise also reject as soon as a promise reject for any reason.
  • any: create a Promise that resolves as soon as one passed from list resolves. It also reject as soon as a promise reject for any reason.
  • pass: Perform an operation in the middle of a chain that does not affect the resolved value but may reject the chain.
  • recover: Allows recovery of a Promise by returning another Promise if it fails.
  • map: Transform items to Promises and resolve them (in paralle or in series)
  • zip: Create a Promise tuple of a two promises
  • defer: defer the execution of a Promise by a given time interval.
  • cancel: cancel is called when a promise is marked as cancelled using operation.cancel()

always

always func is very useful if you want to execute code when the promise fulfills — regardless of whether it succeeds or fails.

showLoadingHUD("Logging in...")
loginUser(username,pass).then { user in
	print("Welcome \(user.username)")
}.catch { err in
 	print("Cannot login \(err)")
}.always {
 	hideLoadingHUD()
}

validate

validate is a func that takes a predicate, and rejects the promise chain if that predicate fails.

getAllUsersResponse().validate { httpResponse in
	guard let httpResponse.statusCode == 200 else {
		return false
	}
	return true
}.then { usersList in
	// do something
}.catch { error in
	// request failed, or the status code was != 200
}

timeout

timeout allows you to attach a timeout timer to a Promise; if it does not resolve before elapsed interval it will be rejected with .timeoutError.

loginUser(username,pass).timeout(.main, 10, .MyCustomTimeoutError).then { user in
	// logged in
}.catch { err in
	// an error has occurred, may be `MyCustomTimeoutError
}

all

all is a static method that waits for all the promises you give it to fulfill, and once they have, it fulfills itself with the array of all fulfilled values (in order).

If one Promise fail the chain fail with the same error.

Execution of all promises is done in parallel.

let promises = usernameList.map { return getAvatar(username: $0) }
all(promises).then { usersAvatars in
	// you will get an array of UIImage with the avatars of input
	// usernames, all in the same order of the input.
	// Download of the avatar is done in parallel in background!
}.catch { err in
	// something bad has occurred
}

If you add promise execution concurrency restriction to all operator to avoid many usage of resource, concurrency option is it.

let promises = usernameList.map { return getAvatar(username: $0) }
all(promises, concurrency: 4).then { usersAvatars in
	// results of usersAvatars is same as `all` without concurrency.
}.catch { err in
	// something bad has occurred
}

any

any easily handle race conditions: as soon as one Promise of the input list resolves the handler is called and will never be called again.

let mirror_1 = "https://mirror1.mycompany.com/file"
let mirror_2 = "https://mirror2.mycompany.com/file"

any(getFile(mirror_1), getFile(mirror_2)).then { data in
	// the first fulfilled promise also resolve the any Promise
	// handler is called exactly one time!
}

pass

pass is useful for performing an operation in the middle of a promise chain without changing the type of the Promise. You may also reject the entire chain. You can also return a Promise from the tap handler and the chain will wait for that promise to resolve (see the second then in the example below).

loginUser(user,pass).pass { userObj in 
	print("Fullname is \(user.fullname)")
}.then { userObj in
	updateLastActivity(userObj)
}.then { userObj in
	print("Login succeded!")
}

recover

recover allows you to recover a failed Promise by returning another.

let promise = Promise<Int>(in: .background, { fulfill, reject in
	reject(AnError)
}).recover({ error in
    return Promise(in: .background, { (fulfill, reject) in
		fulfill(value)
    })
})

map

Map is used to transform a list of items into promises and resolve them in parallel or serially.

[urlString1,urlString2,urlString3].map {
	return self.asyncFunc2(value: $0)
}.then(.main, { dataArray in
	// get the list of all downloaded data from urls
}).catch({
	// something bad has occurred
})

zip

zip allows you to join different promises (2,3 or 4) and return a tuple with the result of them. Promises are resolved in parallel.

zip(a: getUserProfile(user), b: getUserAvatar(user), c: getUserFriends(user))
  .then { profile, avatar, friends in
	// ... let's do something
}.catch {
	// something bad as occurred. at least one of given promises failed
}

defer

As name said, defer delays the execution of a Promise chain by some number of seconds from current time.

asyncFunc1().defer(.main, 5).then...

retry

retry operator allows you to execute source chained promise if it ends with a rejection. If reached the attempts the promise still rejected chained promise is also rejected along with the same source error.
Retry also support delay parameter which specify the number of seconds to wait before a new attempt (2.0.4+).

// try to execute myAsyncFunc(); if it fails the operator try two other times
// If there is not luck for you the promise itself fails with the last catched error.
myAsyncFunc(param).retry(3).then { value in
	print("Value \(value) got at attempt #\(currentAttempt)")
}.catch { err in
	print("Failed to get a value after \(currentAttempt) attempts with error: \(err)")
}

Conditional retry allows you to control retryable if it ends with a rejection.

// If myAsyncFunc() fails the operator execute the condition block to check retryable.
// If return false in condition block, promise state rejected with last catched error.
myAsyncFunc(param).retry(3) { (remainAttempts, error) -> Bool in
  return error.isRetryable
}.then { value in
	print("Value \(value) got at attempt #\(currentAttempt)")
}.catch { err in
	print("Failed to get a value after \(currentAttempt) attempts with error: \(err)")
}

cancel

cancel is called when a promise is marked as cancelled from the Promise's body by calling the operation.cancel() function. See the Cancellable Promises for more info.

asyncFunc1().cancel(.main, {
	// promise is cancelled, do something
}).then...

Chaining Promises with different Value types

Sometimes you may need to chain (using one of the available operators, like all or any) promises which returns different kind of values. Due to the nature of Promise you are not able to create an array of promises with different result types. However thanks to void property you are able to transform promise instances to generic void result type. So, for example, you can execute the following Promises and return final values directly from the Promise's result property.

let op_1: Promise<User> = asyncGetCurrentUserProfile()
let op_2: Promise<UIImage> = asyncGetCurrentUserAvatar()
let op_3: Promise<[User]> = asyncGetCUrrentUserFriends()

all(op_1.void,op_2.void,op_3.void).then { _ in
	let userProfile = op_1.result
	let avatar = op_2.result
	let friends = op_3.result
}.catch { err in
	// do something
}

Installation

You can install Hydra using CocoaPods, Carthage and Swift package manager

  • Swift 3.x: Latest compatible is 1.0.2 pod 'HydraAsync', ~> '1.0.2'
  • Swift 4.x: 1.2.1 or later pod 'HydraAsync'

CocoaPods

use_frameworks!
pod 'HydraAsync'

Carthage

github 'malcommac/Hydra'

Swift Package Manager

Add Hydra as dependency in your Package.swift

  import PackageDescription

  let package = Package(name: "YourPackage",
    dependencies: [
      .Package(url: "https://github.com/malcommac/Hydra.git", majorVersion: 0),
    ]
  )

Consider ❤️ support the development of this library!

Requirements

Current version is compatible with:

  • Swift 5.x
  • iOS 9.0 or later
  • tvOS 9.0 or later
  • macOS 10.10 or later
  • watchOS 2.0 or later
  • Linux compatible environments

Contributing

  • If you need help or you'd like to ask a general question, open an issue.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Copyright & Acknowledgements

SwiftLocation is currently owned and maintained by Daniele Margutti.
You can follow me on Twitter
@danielemargutti.
My web site is https://www.danielemargutti.com

This software is licensed under MIT License.

Follow me on:

Comments
  • Zip with promises array

    Zip with promises array

    Hey, great library! I have been looking for something like this for a while.

    I'd like to suggest that zip should receive an array of promises (if all of them shares the same return type) and a return a promise that combines all the inputs. It could also receive a reduce function as argument. What do you think?

    I mean something like this:

    var promises = [Promise<User>]()
    
    // Some code here. 
    
    Promise.zip(promises)
          .then { (users: [User]) in 
    
          }
    

    Thanks!

    enhancement 
    opened by fmo91 7
  • Promise+Recover.swift does not compile for macOS / Swift 4

    Promise+Recover.swift does not compile for macOS / Swift 4

    I get the following compile error:

    /Users/stefan/Documents/Git-Workspace/Modules/Hydra/Sources/Hydra/Promise+Recover.swift:47:11: error: ambiguous reference to member 'then(in:_:)'
                            return self.then(in: ctx, {
                                   ^~~~
    /Users/stefan/Documents/Git-Workspace/Modules/Hydra/Sources/Hydra/Promise+Then.swift:48:14: note: found this candidate
            public func then<N>(in context: Context? = nil, _ body: @escaping ( (Value) throws -> N) ) -> Promise<N> {
                        ^
    /Users/stefan/Documents/Git-Workspace/Modules/Hydra/Sources/Hydra/Promise+Then.swift:76:14: note: found this candidate
            public func then<N>(in context: Context? = nil, _ body: @escaping ( (Value) throws -> (Promise<N>) )) -> Promise<N> {
                        ^
    /Users/stefan/Documents/Git-Workspace/Modules/Hydra/Sources/Hydra/Promise+Then.swift:118:14: note: found this candidate
            public func then(in context: Context? = nil, _ body: @escaping ( (Value) throws -> () ) ) -> Promise<Value> {
                        ^
    

    I'm using Xcode 9.0 (9A235), swift 4 and a deployment target of 10.12.

    bug 
    opened by stefanhp 6
  • How await works. Support for async function

    How await works. Support for async function

    Hi, cool library 👍

    I was reading through https://github.com/malcommac/Hydra/blob/master/Sources/Hydra/Await.swift#L109

    // Create a semaphore to block the execution of the flow until
    		// the promise is fulfilled or rejected
    		let semaphore = DispatchSemaphore(value: 0)
    

    so we're turning async code -> sync code by blocking the current thread?

    Also, it would be cooler if there's an example project 😉

    enhancement 
    opened by onmyway133 6
  • await does not work on ios11

    await does not work on ios11

    Hey I used to use await to queue up my task executions

    for a in self.aaa {
        try await(self.do[a].execute())
    }
    

    But I upgraded to ios 11 today and run into a EXC_BREAKPOINT here. The call stack is image

    And the error is image

    bug 
    opened by hy9be 4
  • Can't install using Carthage

    Can't install using Carthage

    I am getting next errors

    *** Building scheme "Hydra-macOS" in Hydra.xcodeproj ** CLEAN FAILED **

    The following build commands failed: Check dependencies (1 failure) ** BUILD FAILED **

    The following build commands failed: Check dependencies (1 failure) warning: no umbrella header found for target 'Hydra-macOS', module map will not be generated warning: no umbrella header found for target 'Hydra-macOS', module map will not be generated A shell task failed with exit code 65: ** CLEAN FAILED **

    The following build commands failed: Check dependencies (1 failure) ** BUILD FAILED **

    The following build commands failed: Check dependencies (1 failure)

    bug 
    opened by ivl 3
  • Leaks?

    Leaks?

    Played around with this library a bit and noticed Xcode is reporting quite a few memory leaks using the latest version and with a simple function:

    func testPromise() -> Promise<Int> {
            return Promise<Int>(in: .utility, { (resolve, reject, _) in
                resolve(5)
            })
    }
    
    testPromise().then { result in
            print(result)
    }
    

    screen shot 2017-07-30 at 17 03 32

    bug 
    opened by Fabezi 3
  • How to use async function (example)

    How to use async function (example)

    This will not run:

    image

    not even the print statement runs.

    This however runs:

    image

    Does the async func always need to be used in a chain using methods like .always? The README examples seem to show this is not needed, but I am confused about its actual usage.

    NOTE: the waitUntil func is from Nimble

    Even if there was some sort of problem w/ the waitUntil func, shouldnt the 1st print statement at least execute before it timesout?

    question 
    opened by daviskoh 3
  • Crash on operation: PromiseStatus on iOS but not on macOS

    Crash on operation: PromiseStatus on iOS but not on macOS

    We are using Hydra as part of a client API framework. There is a version without Promise that uses Alamofire for networking. We then add a Promise based wrapper for each API call. The framework exists for both iOS and macOS. Recently we upgraded to Xcode 11.2, swift 5.1 and Hydra 2.0.2 (from 1.2.1, using SPM).

    Now our tests for macOS pass, but the iOS tests crash in Promise.swift, line 71:

    	 public lazy var operation: PromiseStatus = {
    		return PromiseStatus(token: self.invalidationToken, { [weak self] () -> () in
    			self?.set(state: .cancelled)
    		})
    	}()
    

    I tried to debug but could not figure out what is going on.

    Here is a sample project that reproduces the bug.

    The project has two targets, one for iOS and one for macOS. Both use the same source and test swift files. The macOS tests pass but the iOS promise test fails (the standard api test passes, so the api part is ok).

    opened by stefanhp 2
  • .timeout operator keep Promise alive even if it resolves before it expires

    .timeout operator keep Promise alive even if it resolves before it expires

    Object will only be released (deinit) after the time interval of timeout setting. For example: .timeout(50) .dosomething()

    if all the work be done in 1 second, the object will be deinit after 50 seconds.

    Would you please help to check it?

    bug 
    opened by mobilinked 2
  • Add support for cancellable Promise in await operator

    Add support for cancellable Promise in await operator

    Let's say I use the example:

    let asyncFunc = async({ _ -> Int in
      let loggedUser = try await(loginUser(username,pass))
      let followersList = try await(getFollowers(loggedUser))
      let countUnfollowed = try await(unfollow(followersList))
      return countUnfollowed
    }).then({ value in
      print("Unfollowed \(value) users")
    })
    

    How would I make that cancelable?

    enhancement 
    opened by tcurdt 2
  • Promise.zip fails to compile with

    Promise.zip fails to compile with "cannot invoke 'zip' with an argument list of type", Xcode 8.3

    Xcode 8.3 (8E162)

    I'm in the process of evaluating Hydra over PromiseKit, the overall process of converting from one to the other hasn't been too hard, actually cleaned up the code a bit, but I've hit a snag

    When trying to work with zip I keep getting

    Error:(695, 24) cannot invoke 'zip' with an argument list of type '(a: Promise<APIAccessRestrictionState>, b: Promise<APIServiceAccessState>)'

    I've expanded the code a little in an effort to try and solve the issue (and make it easier to read)

    let restrictionStatePromise: Promise<APIAccessRestrictionState> = CioffiAPIManager.shared.getServicesAuthenticatedState()
    let accessStatePromise: Promise<APIServiceAccessState> = CioffiAPIManager.shared.getServiceAccessState()
    
    return Promise<Void>.zip(
    			a: restrictionStatePromise,
    			b: accessStatePromise)
    		.then { (restrictionState: APIAccessRestrictionState,
    		         accessState: APIServiceAccessState) -> Promise<Bool> in
    			self.authorisedState = DefaultAPIAccessRestrictionState(from: state)
    			self.serviceAccessState = state
    			self.authenticationChanged()
    			return Promise<Bool>(resolved: true)
    		}
    
    

    I've also tried using...

    return Promise.zip(
    		a: restrictionStatePromise,
    		b: accessStatePromise)
    
    

    But neither work.

    APIServiceAccessState is a typealias of APIAccessRestrictionState...

    public typealias APIServiceAccessState = APIAccessRestrictionState

    so I'm wondering if that's the issue and if something like all might be better

    opened by RustyKnight 2
  • Method is ambiguous for type lookup in this context

    Method is ambiguous for type lookup in this context

    Need to rename the Hydra enum to something else because it causing the ambiguity error in those projects in which similar namination is used. For eg. In my project I used a class with name Promise and when I try to tell compiler that use the Promise of Hydra by Hydra.Promise<Int> then it start complaing that Hydra Enum doesn't contain a case promise. For more Information check issue

    Relevant Issue

    opened by anshulmahipal 0
  • Importing Hydra in a file where there is a UIViewControllerRepresentable gives compilation errors

    Importing Hydra in a file where there is a UIViewControllerRepresentable gives compilation errors

    Hi everyone! I'm using Hydra 2.0.6 on iOS on Xcode 13.2.1.

    I would like to import Hydra on this file

    import SwiftUI
    
    struct VideocallViewControllerWrapper: UIViewControllerRepresentable {
        ...
    }
    

    but as soon as I import it, Xcode tells me that Type 'VideocallViewControllerWrapper' does not conform to protocol 'UIViewControllerRepresentable'. I have other files where I have successfully imported both SwiftUI and Hydra. I think the problem is the presence of UIViewControllerRepresentable.

    Thank you

    opened by fabiocody 0
  • Is the minimum requirement still iOS 9?

    Is the minimum requirement still iOS 9?

    I'm using Hydra 2.0.6 with Xcode 13, but at compile time I get a message that Hydra requires iOS 12 or higher. But I didn't have any problems when using Xcode 12 and Hydra 2.0.5. Did the minimum requirements change? Thank you.

    Showing Recent Messages /Users/hoge/Test/ViewController.swift:11:8: Compiling for iOS 10.0, but module 'Hydra' has a minimum deployment target of iOS 12.0: /Users/hoge/Library/Developer/Xcode/DerivedData/Test-akvdfnkqlvyyxafhnfgahlouszoz/Build/Products/Debug-iphonesimulator/Hydra.framework/Modules/Hydra.swiftmodule/x86_64-apple-ios-simulator.swiftmodule

    opened by toshihiko150 0
  • Retry vs Delay

    Retry vs Delay

    Hi! First of all, I have to say that I really love this library 🙏 The reason for this issue is cause I'm not sure if I found a bug or if I misunderstood the semantics of retry with delay.

    Context

    I was trying to implement polling to a web server leveraging Promises. Say we have promise: Promise<Value> which encapsulate the async call to our backend. Then I expressed polling with something like:

    promise
      .validate { value in /* predicate to decide if continue polling */ }
      .retry(10, delay: pollingTime)
    

    Expectation

    Since:

    • validate rejects the promise if the predicate is not met
    • retry allow to execute source chained promise if it ends with a rejection (exactly what I need, to re-issue the web call)
    • delay is the delay between each attempt (starting when failed the first time)

    My expectation was to retry the promise at most 10 times, each with a delay of pollingTime.

    Result

    AFAIU, the delay is implemented with the defer which in turn defers only the happy path (then) but the retryWhen is actually targeting a rejected promise. This results in the delay being applied in the only attempt that succeeds (if any).

    I've also tried to encapsulate the validate inside the promise (rejecting it if the predicate is not met) but it didn't move the problem. I could achieve what I expected only pushing the validation inside the promise and implementing a deferAll:

      func `deferAll`(in context: Context = .background, _ seconds: TimeInterval) -> Promise<Value> {
        guard seconds > 0 else { return self }
    
        let fireTime: DispatchTime = .now() + seconds
        return self
          .then(in: context) { value in return Promise<Value> { resolve, _, _ in
            context.queue.asyncAfter(deadline: fireTime) { resolve(value) }
          }}
          .recover { error in return Promise<Value> { _, reject, _ in
            context.queue.asyncAfter(deadline: fireTime) { reject(error) }
          }}
      }
    }
    

    and leveraging it inside the retryWhen in place of the defer.

    Am I misunderstanding the semantic of retry with delay?

    Thank you 😄

    opened by AlessandroSteri 0
  • Ubuntu compilation error

    Ubuntu compilation error

    /home/user/Project/.build/checkouts/Hydra.git-8391472609009747209/Sources/Hydra/Promise+Await.swift:99:20: error: binary operator '!=' cannot be applied to two 'DispatchQueue' operands
                    guard self.queue != DispatchQueue.main else {
                          ~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~
    
    Swift version 5.0.1 (swift-5.0.1-RELEASE)
    Target: x86_64-unknown-linux-gnu
    
    Ubuntu 16.04.6 LTS
    
    opened by sage444 3
  • Progress block

    Progress block

    Hi Malcom! Is there any kind of progress block for things like uploading an avatar for example.

    uploadAvatar()
    .progress { p in
      // Here update progressView for example
    }
    .then(doSomething)
    .onError(showErrorPopup)
    .finally(doSomething)
    
    opened by gloryluu 0
Releases(2.0.6)
  • 2.0.6(Apr 25, 2021)

    Released on: 2021-04-25

    Changes

    • [#86] [ENH] Suppress await() function in Xcode 12.5. Now you can use Hydra.await() instead in order to avoid any warning on lib.
    Source code(tar.gz)
    Source code(zip)
  • 2.0.5(Oct 8, 2020)

    Released on: 2020-10-08

    Changes

    • [#83] [FIX] Removed weak self from recover operator so the recover's parent promise is strongly retained
    • [NEW] Added support for Swift Package Manager 5.3
    • [NEW] Minimum deployment target set to iOS 9
    Source code(tar.gz)
    Source code(zip)
  • 2.0.4(Sep 15, 2020)

    Released on: 2020-09-15

    Changes

    • [NEW] #71 - Adding support for delay parameter in retry() operator
    • [FIX] #81 - Compatibility with Xcode 12, fixed warning and update project settings enhancement
    • [FIX] #77 - Fixed chainable thens operators in Xcode 12/Swift 5.3
    Source code(tar.gz)
    Source code(zip)
  • 2.0.3(Jul 23, 2020)

  • 1.2.2(Jul 22, 2020)

  • 2.0.0(Mar 29, 2019)

  • 1.2.1(Oct 27, 2017)

  • 1.2.0(Oct 27, 2017)

  • 1.0.2(Oct 27, 2017)

  • 1.0.1(Oct 27, 2017)

  • 1.1.0(Sep 14, 2017)

  • 1.0.0(Sep 3, 2017)

    • Release Date: 2017-09-03

    • Zipped Version: Download 1.0.0

    • #45 Added support for cancellable promises inside the await operator.

    • #46 Resolved an issue where timeout operator keep a Promise alive even if it resolves correctly before it expires.

    • #44 Resolved a compatibility issue with await under iOS 11 or later.

    • #48 Resolved a memory leaks with cancellable promises.

    • #49 Replaced with (Void) with () to fix warnings with Swift 4 and XCode 9

    Source code(tar.gz)
    Source code(zip)
  • 0.9.9(Jul 24, 2017)

  • 0.9.7(Jul 9, 2017)

    Important Notice

    Since 0.9.7 Hydra implements Cancellable Promises. In order to support this new feature we have slightly modified the Body signature of the Promise; in order to make your source code compatible you just need to add the third parameter along with resolve,reject: operation. operation encapsulate the logic to support Invalidation Token. It's just and object of type PromiseStatus you can query to see if a Promise is marked to be cancelled from the outside. If you are not interested in using it in your Promise declaration just mark it as _.

    To sum up your code:

    return Promise<Int>(in: .main, token: token, { resolve, reject in ...
    

    needs to be:

    return Promise<Int>(in: .main, token: token, { resolve, reject, operation in // or resolve, reject, _
    

    New Features:

    Source code(tar.gz)
    Source code(zip)
  • 0.9.5(Jun 10, 2017)

  • 0.9.4(Mar 21, 2017)

    • #22 Fixed an issue with any operator which in several occasions does not work.
    • #24 Fixed never chainable if return rejected promise in recover operator closure.
    • #26 Added concurrency argument in all operator; it allows you to set the number of max concurrent promises running for an all task in order to reduce memory and cpu footprint.
    • #28 Fixed Promise's memory leak
    • #29 Cleaned up the process of running the observer
    • #18, #20 Refactoring of several func inside the Promise's class.
    Source code(tar.gz)
    Source code(zip)
  • 0.9.3(Mar 6, 2017)

  • 0.9.0(Feb 4, 2017)

    First Public Release

    This is the first public release of Hydra. All tests are passed the library supports the following features:

    • Promise class support following operators:
      • always
      • valide
      • timeout
      • any
      • all
      • pass
      • recover
      • map
      • zip
      • defer
      • retry
    • Initial support for await operator (or ..!,.., see the doc)
    Source code(tar.gz)
    Source code(zip)
Owner
Daniele Margutti
iOS & macOS Engineer, UI/UX Lover. Tech Lead at Immobiliare.it. Continuous improvement is better than delayed perfection.
Daniele Margutti
When is a lightweight implementation of Promises in Swift.

Description When is a lightweight implementation of Promises in Swift. It doesn't include any helper functions for iOS and OSX and it's intentional, t

Vadym Markov 260 Oct 12, 2022
Lightweight promises for iOS, macOS, tvOS, watchOS, and Linux

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

David Ask 60 Aug 11, 2022
FutureLib is a pure Swift 2 library implementing Futures & Promises inspired by Scala.

FutureLib FutureLib is a pure Swift 2 library implementing Futures & Promises inspired by Scala, Promises/A+ and a cancellation concept with Cancellat

Andreas Grosam 39 Jun 3, 2021
A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

null 759 Dec 2, 2022
A promises library written in Swift featuring combinators like map, flatMap, whenAll, whenAny.

Promissum is a promises library written in Swift. It features some known functions from Functional Programming like, map and flatMap. It has useful co

Tom Lokhorst 68 Aug 31, 2022
Futures and Promises library

#PureFutures A simple Futures and Promises library. ##Installation ###Carthage Add the following in your Cartfile: github "wiruzx/PureFutures" And ru

Victor Shamanov 17 Apr 5, 2019
Promises for Swift & ObjC.

Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master and result in

Max Howell 14k Jan 5, 2023
Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Thomas Visser 1.9k Dec 20, 2022
The easiest Future and Promises framework in Swift. No magic. No boilerplate.

Promis The easiest Future and Promises framework in Swift. No magic. No boilerplate. Overview While starting from the Objective-C implementation of Ju

Alberto De Bortoli 111 Dec 27, 2022
Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.

Promises Promises is a modern framework that provides a synchronization construct for Objective-C and Swift to facilitate writing asynchronous code. I

Google 3.7k Dec 24, 2022
Easy Swift Futures & Promises.

❗️ Archived now ❗️ Since Apple released Combine framework, I decide to archive this repo. You still can use this repo as an example of Future/Promise

Dmytro Mishchenko 40 Sep 23, 2022
NotificationCenter based Lightweight UI / AnyObject binder.

Continuum NotificationCenter based Lightweight UI / AnyObject binder. final class ViewController: UIViewController { @IBOutlet weak var label: UI

Taiki Suzuki 82 Apr 4, 2021
A Promise library for Swift, based partially on Javascript's A+ spec

Promise A Promise library for Swift, based partially on Javascript's A+ spec. What is a Promise? A Promise is a way to represent a value that will exi

Soroush Khanlou 622 Nov 23, 2022
A library that adds a throwing unwrap operator in Swift.

ThrowingUnwrap A simple package to add a throwing unwrap operator (~!) to Optionals in Swift. Import Add this to the package-wide dependencies in Pack

Allotrope 3 Aug 27, 2022
A light-weighted Promise library for Objective-C

RWPromiseKit Desiciption A light-weighted Promise library for Objective-C About Promise The Promise object is used for deferred and asynchronous compu

Canopus 113 May 4, 2022
Promise + progress + pause + cancel + retry for Swift.

SwiftTask Promise + progress + pause + cancel + retry for Swift. How to install See ReactKit Wiki page. Example Basic // define task let task = Task<F

ReactKit 1.9k Dec 27, 2022
Promise/A+, Bluebird inspired, implementation in Swift 5

Bluebird.swift Promise/A+ compliant, Bluebird inspired, implementation in Swift 5 Features Promise/A+ Compliant Swift 5 Promise Cancellation Performan

Andrew Barba 41 Jul 11, 2022
Material para a apresentação da palestra "Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS" no TDC Future 2021

--- title: Implementando Interesses Transversais - um papo sobre arquitetura, DI e Design Patterns em Swift/iOS author: Cícero Camargo date: Nov 30th

Cícero Camargo 2 Nov 30, 2021
PromiseKit 4.5.2 with changes for Swift 5

繁體中文, 简体中文 Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master an

null 0 Dec 24, 2021