Search code examples
swiftpromisekit

Creating a void Promise which wraps executable code


I need to wrap some code in a promise using PromiseKit. I can create a void promise like this:

let promise = Promise(value:()) // Void promise. 

But that's immediately resolved where as I want to return a void promise that wraps some code which is executed as part of the normal promise chain. I can do this:

let promise = Promise<Void> {fulfilled, _ in
    // Here is where I do stuff.
    fulfill()
}

But this feels kinda clunky.

Is there a more succinct way to generate a void promise that executes code?


Solution

  • I do the same. I think it's the only existing way. If you don't wan't to create promise object you can do following:

    self.client.delete(device: device)
        .then { result -> Promise<Void> in
             // do something with result
             return Promise(value: ())
        }
        .catch {
             // handle
    }