Like many developers I got an email from Parse last night announcing a new library called Bolts, which provides low-level tools for use in iOS and Android applications. At the moment it consists of a sole offering that is substantial in itself – a promises API called “tasks”. Having seen the usefulness of promises in a few places recently, most notably AngularJS, the prospect of using them in an iOS application is an exciting one.

Using tasks makes the following approach possible (here, in Objective-C for iOS):

PFQuery *query = [PFQuery queryWithClassName:@"Comments"];
[query whereKey:@"post" equalTo:@123];
[[[self findAsync:query] continueWithBlock:^id(BFTask *task) {
  NSArray *results = task.result;
  // Create a trivial completed task as a base case.
  BFTask *task = [BFTask taskWithResult:nil];
  for (PFObject *result in results) {
    // For each item, extend the task with a function to delete the item.
    task = [task continueWithBlock:^id(BFTask *task) {
      // Return a task that will be marked as completed when the delete is finished.
      return [self deleteAsync:result];
return task;
}] continueWithBlock:^id(BFTask *task) {
  // Every comment was deleted.
  return nil;

This code block makes an initial asynchronous call which subsequently fires two callbacks in series. The callbacks themselves may also execute asynchronous code, which will delay the execution of subsequent callbacks.

The nice thing about this approach is it eschews nesting and error checking, with each continueWithBlock statement only being executed in the event that the previous one succeeded. Objective-C can be verbose enough without going three levels or more deep into a nested callback structure, as it’s easy to do when working with Core Data contexts on a background thread, for example. I’m already looking forward to trying this out in the next iOS app I work on, and am eagerly anticipating future additions to the Bolts toolkit.