Shameless plug for my tiny implementation of Promise (similar to the Future type mentioned in the article)[1].<p>I've used it in a few projects, and it makes dealing with async things feel surprisingly natural:<p><pre><code> getLocation()
.then { apiRequest(params, location: $0) }
.then { json -> () in
for result in json["results"] {
display(result)
}
}
.catch { showError($0) }
</code></pre>
It's also easy trivial to adapt existing callback-based[2] and delegate-based[3] APIs to promises.<p>[1]: <a href="https://gist.github.com/Sidnicious/93d2a8db1266e2fd239c" rel="nofollow">https://gist.github.com/Sidnicious/93d2a8db1266e2fd239c</a><p>[2]: <a href="https://gist.github.com/Sidnicious/69df2c483aadc08bda4f" rel="nofollow">https://gist.github.com/Sidnicious/69df2c483aadc08bda4f</a><p>[3]: <a href="https://gist.github.com/Sidnicious/99b099d9b6f2ecb9e383" rel="nofollow">https://gist.github.com/Sidnicious/99b099d9b6f2ecb9e383</a>
The Failable type in the article looks like this Result project on GitHub [1]. It makes for interesting related reading.<p>[1]: <a href="https://github.com/antitypical/Result" rel="nofollow">https://github.com/antitypical/Result</a>
> LOGGING FROM DIFFERENT BACKGROUND THREADS AT ONCE<p>Or, you could just make a logging serial queue that you send all log messages to; that way there's no risk of activity on the main thread slowing down your logging. In (Obj-)C:<p><pre><code> static queue_t queue;
void log (NSString *message) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("logging", DISPATCH_QUEUE_SERIAL);
});
dispatch_async(queue, ^{
NSLog(message);
});
}</code></pre>