Promise in swift? Write your own

What is Promise?
To me, Promise is just to make async call in a linguistic representation for human read.
Think about it:

somethingAsync{ ok in
    use(ok)
    ...
    anotherAsync{ ok2 in
       ...
    }
}
It help when we can organise them into:
when{ done in
    somethingAsync{ value in
        done(value)
    }
} ==> 
(ok:{ value in
    anotherAsync(value)
},fail:{
    print("omg")
})
Even stronger:
when{ done in
    somethingAsync{ value in
        done(value)
    }
} <== { keywords in
    return { c:String?->() in 
        anotherAsync{
            let value = findNumberWithKeyword(keyword)
            c(value)
        }
    }
} <== { numericString in
    return { c:Double?->() in
        if canConvertToNumber(numericString) {
            let converted = toDouble(numericString)
            c(converted)
        }else{
            c(nil)
        }
    }
} ==> 
(ok:{ value in
    /// lets use the value!!
},fail:{
    /// ops... 
})

And why you should clap your hand here is: There are only functions!
No class, no structure, pure function.
Ok, time to get your hand dirty, try to implement it in swift.
I will post out my solution later. See you.

Original post from my previous site: http://povoq.blogspot.com/2017/07/promise-in-swift-write-your-own.html

Subscribe to TechRD.in

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe