Giter Club home page Giter Club logo

then's Introduction

then ๐ŸŽฌ

Language: Swift 2 Platform: iOS 8+ Carthage compatible Cocoapods compatible Build Status codebeat badge Join the chat at https://gitter.im/s4cha/then License: MIT Release version

Elegant Async code for Swift

fetchUserId().then { id in
    print("UserID : \(id)")
}.onError { e in
    print("An error occured : \(e)")
}.finally {
    print("Everything is Done :)")
}

Before

fetchUserId({ id in
    fetchUserNameFromId(id, success: { name in
        fetchUserFollowStatusFromName(name, success: { isFollowed in
            // The three calls in a row succeeded YAY!
            reloadList()
        }, failure: { error in
            // Fetching user ID failed
            reloadList()
        })
    }, failure: { error in
        // Fetching user name failed
        reloadList()
    })
}) {  error in
    // Fetching user follow status failed
    reloadList()
}
๐Ÿ™‰๐Ÿ™ˆ๐Ÿ™Š#callbackHell

After

fetchUserId()
    .then(fetchUserNameFromId)
    .then(fetchUserFollowStatusFromName)
    .then(updateFollowStatus)
    .onError(showErrorPopup)
    .finally(reloadList)

๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰

Why

Because async code is hard to write, hard to read, hard to reason about.
A pain to maintain

How

By using a then keyword that enables you to write aSync code that reads like an English sentence
Async code is now concise, flexible and maintainable โค๏ธ

What

  • Based on the popular Promise/Future concept
  • Lightweight (1 file ~100lines)
  • Pure Swift
  • No magic involved
  • Strongly Typed
  • Chainable

Installation

Cocoapods

pod 'thenPromise'
use_frameworks!

Carthage

github "s4cha/then"

Manually

Simply Copy and Paste Promise.swift in your Xcode Project :) https://github.com/s4cha/then/blob/master/Code/then/Promise.swift

As A Framework

Grab this repository and build the Framework target on the example project. Then Link against this framework.

Example

fetchUserId().then { id in
    print("UserID : \(id)")
}.onError { e in
    print("An error occured : \(e)")
}.finally {
    print("Everything is Done :)")
}

Going further ๐Ÿค“

If we want this to be maintainable, it should read like an English sentence
We can do this by extracting our blocks into separate functions:

fetchUserId()
    .then(printUserID)
    .onError(showErrorPopup)
    .finally(reloadList)

This is now concise, flexible, maintainable, and it reads like an English sentence <3
Mental sanity saved // #goodbyeCallbackHell

Writing your own Promise ๐Ÿ’ช

Wondering what fetchUserId() is?
It is a simple function that returns a strongly typed promise :

func fetchUserId() -> Promise<Int> {
    return Promise { resolve, reject in
        print("fetching user Id ...")
        wait { resolve(object: 1234) }
    }
}

Here you would typically replace the dummy wait function by your network request <3

Contributors

S4cha, YannickDot, Damien, piterlouis

Other repos โค๏ธ

then is part of a series of lightweight libraries aiming to make developing iOS Apps a breeze:

then's People

Contributors

s4cha avatar gitter-badger avatar tmcw avatar

Stargazers

Anton Nechaiuk avatar

Watchers

Pete Morales avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.