# Getting Started with PromiseKit

**URL:** <https://forums.kodeco.com/t/getting-started-with-promisekit/24503>\
**Category:** 🎓 Courses & Articles\
**Created:** [December 26, 2016, 2:03pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503 "2016-12-26T14:03:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkatz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mkatz/32/108269_2.png) [@mkatz](https://forums.kodeco.com/u/mkatz)\
**Post date:** [December 26, 2016, 2:03pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/1 "2016-12-26T14:03:58Z")

</div>

Asynchronous programming can be a real pain in the lemon. Unless you’re extremely careful, it can easily result in humongous delegates, messy completion handlers and long nights debugging code! Lucky for you, there’s a better way: promises. Promises tame asynchronicity by letting you write code as a series of actions based on events. This works […]

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/786-getting-started-with-promisekit](https://www.raywenderlich.com/786-getting-started-with-promisekit)

---

<div class="post-metadata">

**Author:** ![qualitywolf](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/qualitywolf/32/52769_2.png) [@qualitywolf](https://forums.kodeco.com/u/qualitywolf)\
**Post date:** [December 28, 2016, 12:09am UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/2 "2016-12-28T00:09:42Z")

</div>

How does URLDataPromise ensure that asDictionary() is not called until it completes?

//1  
let dataPromise: URLDataPromise = session.dataTask(with: request)

```
// 2
_ = dataPromise.asDictionary().then { dictionary -> Void in

```

---

<div class="post-metadata">

**Author:** ![chrisjl](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/chrisjl/32/110937_2.png) [@chrisjl](https://forums.kodeco.com/u/chrisjl)\
**Post date:** [December 30, 2016, 1:36pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/3 "2016-12-30T13:36:29Z")

</div>

`.asDictinary()` probably doesn’t actually _do_ anything other than register for something to happen later, when the data does arrive.

For example, this hastily written skeleton of something vaguely Promise-ish:

```
struct Xyz {}

class Promise {

    var transformFunc: ((Any) -> Any)?;
    var data: Any?;

    static func promiseFetchingData(fromUrl url: String) -> Promise {
        let p = Promise()
        p.getData(fromUrl: url)
        return p
    }

    func getData(fromUrl url: String) {
        // Make http request, store result in self.data, or pass to transform and .then, if already available
    }

    func asDictionary() -> Promise {
        transformFunc = transformToDictionary
        return self
    }

    func asXyz() -> Promise {
        transformFunc = transformToXyz
        return self
    }

    func then(handler: (Any) -> ()) {
        if let t = transformFunc {
            data = t(data)
        }
        handler(data)
    }

    func transformToDictionary(data: Any) -> Dictionary<String,Any> {
        return ["key": 1234, "other": 5678]
    }

    func transformToXyz(data: Any) -> Xyz {
        return Xyz()
    }

}

_ = Promise.promiseFetchingData(fromUrl: "http://myserver.net/api/1234" )
            .asDictionary()
            .then {
                dict in
                // Do stuff
                print(dict)
            }

```

Real code would need to allow for data returning _before_ the transformation or the `.then()` were registered.

---

<div class="post-metadata">

**Author:** ![frzi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/frzi/32/56530_2.png) [@frzi](https://forums.kodeco.com/u/frzi)\
**Post date:** [February 4, 2017, 8:27pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/4 "2017-02-04T20:27:59Z")

</div>

A (good) while ago I made my own bare bone and easy-to-use Promise class for Swift: [Swift Promises (Github)](https://github.com/frzi/SwiftPromises)  
Updated every so slightly here and there since the Swift 2 days to accommodate for the language’s changes.

Fair warning, it is _really_ bare bones. But I’ve been using it for practically all of the iOS apps I’ve worked on in the past 12+ months and has served me really well for async related tasks.

It was interesting how developers were already creating their own Promise libraries for Swift since the beta days. Both PromiseKit and BrightFutures are the names I’ve seen pop-up most over the years. And I’m glad your article has gone in-depth with one of them. It certainly convinced me to give it a try next time. (Or maybe help me improve my own simple Promise class. 🙂 ) Cheers!

---

<div class="post-metadata">

**Author:** ![mkatz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mkatz/32/108269_2.png) [@mkatz](https://forums.kodeco.com/u/mkatz)\
**Post date:** [February 5, 2017, 3:11pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/5 "2017-02-05T15:11:22Z")

</div>

@frzi

One nice thing about all these open source libraries is that you can always pick and choose parts of it to upgrade your own implementation as the need arises.

---

<div class="post-metadata">

**Author:** ![evan\_0\_0](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/evan_0_0/32/81494_2.png) [@evan\_0\_0](https://forums.kodeco.com/u/evan_0_0)\
**Post date:** [June 22, 2017, 4:18am UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/6 "2017-06-22T04:18:43Z")

</div>

@mkatz I downloaded the sample project and found that it keeps showing an warning – “This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.”(iOS Deployment Target 10.0 & Xcode 8.3.3).

I think you probably should fix this by putting the \> completion(result, nil) within **DispatchQueue.main.async { … }** in function **getWeatherTheOldFashionedWay(latitude:, longitude:, completion:)**.

---

<div class="post-metadata">

**Author:** ![mkatz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mkatz/32/108269_2.png) [@mkatz](https://forums.kodeco.com/u/mkatz)\
**Post date:** [July 6, 2017, 11:14pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/7 "2017-07-06T23:14:00Z")

</div>

Thanks, what a boneheaded move! We’ll get that on the next update round.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 16, 2017, 11:12pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/8 "2017-11-16T23:12:04Z")

</div>

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 16, 2017, 11:12pm UTC](https://forums.kodeco.com/t/getting-started-with-promisekit/24503/9 "2017-11-16T23:12:06Z")

</div>

Closed on November 16, 2017, 11:12pm UTC


