Skip to content

Interest in aligning with ES6 Promise semantics? #21

Description

@stefanpenner

I understand this is objective-c land, but as promises in JavaScript are now part of a formal standard, is there interest in aligning this project with those semantics?

One very specific and confusing difference is how the promises compose.

in ES6: there is no concept of a promise fulfilling with another promise, when a promise is to resolve another promise, it merely assimilates the new promises state.

Simple example:

var rejectedPromise = Promise.reject(new Error);
var promise = Promise.resolve(rejectedPromise)

promise.then(function(){
  // will not invoke
}).catch(reason) {
  // will invoke, with the reason of the assimilated promise
  // to propagate the rejection
  throw reason;

  // return to "catch" and handle the rejection, providing a new success value"
  return 1;

  // return a promise, to "retry"
  return User.find(1);
}); 

Why is this useful?

It encourages encapsulation, and mimics synchronous programmings try/catch

example continued:

sync:

function fetch() {
  var something = ajax('/something');
  var somethingElse = ajax('/something-else/' + something.id);
  // if either attempt throws, fetch throws
  return something;
}

async

function fetch() {
  return ajax('/something').then(function(value) {
    return ajax('/something-else/' + value.id);
  });
}
// if either attempt rejects, the promise returned from fetch rejects

// refactor further
function fetchSomethingElse(value) {
  return ajax('/something-else/' + value.id);
}

function fetch() {
  return ajax('/something').then(fetchSomthingElse);
}
// identical semantics.

To summarize, these chaining and assimilation semantics encourage best practices. Although this library is clearly not JavaScript, aligning only improves mindshare on the topic.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions