paint-brush
sWrapping up APIs with PromiseKitby@raymund.cat
1,103 reads
1,103 reads

sWrapping up APIs with PromiseKit

by john raymund catahayNovember 12th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

New to Promises and how to use them in iOS development? then please feel free read this short introduction I wrote previously on this topic: <a href="https://hackernoon.com/promise-pattern-on-swift-with-promisekit-620efad7a5bd" target="_blank">https://hackernoon.com/promise-pattern-on-swift-with-promisekit-620efad7a5bd</a>

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - sWrapping up APIs with PromiseKit
john raymund catahay HackerNoon profile picture

Actually just some code snippets I’d like to share with you

New to Promises and how to use them in iOS development? then please feel free read this short introduction I wrote previously on this topic: https://hackernoon.com/promise-pattern-on-swift-with-promisekit-620efad7a5bd

Promises are fun I swear!

On my previous article, I showed some examples on you how you can convert your async call pyramids into a more organised chain of promises like this:

More so, wrap them up without changing your original API implementations and end up with something like this:

The above implementation was possible by creating a single reusable wrapper function that provides you the closure you need when handling async callbacks:

The wrapper function holds reference to the completion block that you can give to your async calls and then return you a Promise object that you can listen to instead. Here’s a more detailed implementation of our custom wrapper:

With this in mind, let’s see how else can we tidy up in our code ~

Involving Third parties in your flow

Social media logins are pretty common in application designs and have even been quite a staple user requirement to most of our apps. Because of this, Facebook SDK’s LoginManager and GraphAPI have been two very familiar libraries that are often part of our architecture.

Third party SDKs are neat for several reasons:

  1. Saves you and your team a LOT of time when you don’t have to reinvent the wheel.
  2. Reliable Third parties mean they maintain and test their code very well.
  3. Libraries that are public and used by thousands~ of people mean issues are found and resolved more easily.

But of course they may also come with some drawbacks, especially:

  1. SDKs are often HUGE. So many things you often don’t need to see.
  2. They are also often designed way differently than your team’s. Be prepared to abstract.

And Facebook SDK is not any different. In this very particular case, we will be looking at how we may integrate Facebook API calls into our Promises chain.

API Example: FB LoginManager

What makes Facebook an easy Social Integration to apps is LoginManager.

But it having its own a async callback may create some problems with your data flow especially when you have already designed them through Promises. Let’s take a look at its ‘completion’ callback and see how can we design a wrapper for it.

What we want here is to be able to replace the completion closure ‘((LoginResult) -> Void)? = nil’ with one that is from our wrapper function.

So first, let’s declare our new wrapper that takes a completion block whose parameter type is the same as the login completion:

Next, let’s prepare the Promise object by using the completion block parameter to trigger the Promise’s fulfil() and reject(). And while we’re at it, let’s go ahead and return the resulting Promise, using the LoginResult’s successful object type of course.

And when that’s all well wrapped up, you should be able to chain your Facebook login calls just like this:

Voila, no need to reimplement anything. Just wrap them up :D

What’s next?

Having built so many Restful Apps, my team and I are so used to organising and chaining asynchronous calls neatly with PromiseKit whenever we get the chance.

After what we’ve learned here, I’m encouraging you to look through your code base as well to hunt down those pesky async call pyramids and see a good solution for cleaning them up.

I wish you best of luck!

You made it all the way here!

I hope that you got what you’re looking for when you opened this article. If so, I would very much appreciate it if you would recommend this to your friends ^^

If you have any more questions/feedbacks, please feel free to drop a message!