Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Hydra_Internals_9.swift
public func retry(_ attempts: Int = 3) -> Promise<Value> {
guard attempts >= 1 else {
return Promise<Value>(rejected: PromiseError.invalidInput)
}
var remainingAttempts = attempts
let nextPromise = Promise<Value>(in: self.context) { (resolve, reject) in
let onResolve = Observer<Value>.onResolve(self.context, resolve)
let onReject = Observer<Value>.onReject(self.context, { error in
remainingAttempts -= 1
guard remainingAttempts >= 0 else {
reject(error)
return
}
self.resetState()
self.runBody()
})
self.add(observers: onResolve,onReject)
self.runBody()
}
nextPromise.runBody()
return nextPromise
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment