monadic flatMap #50

Closed
Raynos opened this Issue Dec 21, 2013 · 8 comments

Projects

None yet

3 participants

@Raynos
Raynos commented Dec 21, 2013

I believe ES6 promises is going to have a flatMap that does not do recursive flattening but instead flattens one level deep. it may also disable thenable assimilation on flatMap.

It would be nice to have something similar here.

@petkaantonov
Owner

Are the semantics laid out somewhere where I can read? I cannot find them in promises_unwrapping repository.

@Raynos
Raynos commented Dec 22, 2013

Not sure what the actual semantics are.

If it's the monadic flatMap then the semantics are pretty much

function flatMap(p, transform) {
  return new Promise(function (resolve, reject) {
    p.then(function (value) {
      var newPromise = transform(value)
      resolve(newPromise.valueOf()) 
    }, reject);
  });
}

i.e. flatMap(p, function (value) { return promise }) it does one level of promise unwrapping.

very similar to chain from fantasy land

@petkaantonov
Owner

So transform returns a promise that is already resolved? How would you use this in asynchronous programming? Or how can you call .valueOf() (Which I assume works like .inspect().value()) ?

@domenic
domenic commented Dec 22, 2013

This is inaccurate; ES6 promises will have no such method. ES6 promises are API complete.

@Raynos
Raynos commented Dec 22, 2013

@domenic flatMap was removed? :/

@Raynos
Raynos commented Dec 22, 2013

@petkaantonov sorry it does not return one that is already resolved. I did it wrong. Implementing flatMap is more involved and can't be done with then.

@domenic
domenic commented Dec 22, 2013

flatMap was never part of ES6 promises. There are some members of the committee that want flatMap, but others who do not; the question will be revisited in ES7.

@Raynos
Raynos commented Dec 23, 2013

Closing this. We can re-open it to prototype ES7 ideas later.

@Raynos Raynos closed this Dec 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment