5

a method in chrome browser named Promise.prototype.chain. I cannot find any documentation about this method. Anybody know anything about it?

btw can i find the documentations of google chrome for all methods in the browser?

LCB
  • 971
  • 9
  • 22

1 Answers1

3

Note: Now .chain is just a deprecated alias for then.


What chain does is basically what then does except not unwrapping promises. Chain is a proper "monadic flatMap" rather than then which automatically unwraps promises.

It was discussed at great lengths on the mailing list and in TC meetings. It was decided against.

.chain is just a relic of the past in Chrome, it might make it in a future release but currently no one is championing a proposal including it. I would not use it in production.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 1
    What does "not unwrapping promises" mean and when would you want to use `.chain()` instead of `.then()`? – jfriend00 Jan 11 '16 at 16:05
  • @jfriend00 Try `Promise.resolve(3).chain(x => ({then: f => f(4) }) ).chain(c => console.log("Got", c))` vs `Promise.resolve(3).then(x => ({then: f => f(4) }) ).then(c => console.log("Got", c))` - it does not automatically unwrap the result. – Benjamin Gruenbaum Jan 12 '16 at 12:38
  • If you wanted to return a promise by itself that was not managed as part of the current chain, you could also just embed it into an object and return that object, right? Perhaps since I'm so used to how it works now with `.then()`, I can't think of a situation where I'd want `.chain()` logic and, even if I could, I could just hide the promise in another returned object and still use `.then()`. Are there any other reasons for `.chain()`? – jfriend00 Jan 12 '16 at 22:26
  • Right, you could embed it into an object and return the object. There are some motivating example in the esdiscuss thread I linked to in the answer. – Benjamin Gruenbaum Jan 12 '16 at 22:28