Skip to content

Instantly share code, notes, and snippets.

@spion
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/8814197 to your computer and use it in GitHub Desktop.
Save spion/8814197 to your computer and use it in GitHub Desktop.

Not sure if this is correct, but: This might actually be (perhaps accidentally) one step closer to allowing promises for thenables in a cleaner way if the need arises in the future.

Since its impossible to create a promise for a promise now, it would be theoretically possible to just

  • introduce Promise.of (wraps thenables)
  • make .then unwrap "true" promises only once
  • make resolve cast thenables into promises that recursively unwrap thenables

and be done.� You'd have

  • Promise.of - wraps thenables, returns promises, wraps other values
  • resolve - converts thenables to recursively unwrapping promises, returns promises, wraps other values
  • .then(f, r) - for values returned by the handlers: recursively unwraps thenables, unwraps promises once, wraps other values

After that, you could use .then in a monadic way, provided you remember to protect thenables with Promise.of (should not be a problem with typed compile-to-js languages). e.g. resolve(Promise.of(thenable)) or p.then(x => Promise.of(thenable)) You could also implement .map using .then and Promise.of

Previously you'd have to make resolve and Promise.cast the same before making .then unwrap a single level. Alternatively, you'd have to introduce .chain and have a confusingly dual API (chain/then, resolve/cast).

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