Q.map #234

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

abrkn commented Mar 11, 2013

I use this pattern all the time with producer-consumer patterns, http://jsfiddle.net/mdfLc/

Collaborator

domenic commented Mar 11, 2013

I agree this could be quite useful, but then again, we're already fighting API surface area bloat. We'll see what @kriskowal says.

In the meantime, you may enjoy Qx, which someone has put together with lots of useful promise + array stuff.

Owner

kriskowal commented Mar 11, 2013

Don’t do any further work until I firmly say whether I’m committed to the change and the name. One data point is that I’ve experimented with adding this feature at least three times and at some point exiled the work to branches.

If I do commit to adding this feature, it will open a flood-gate for related features. I will need to be comfortable with the overall vision. That would entail having Q.reduce and Q.forEach with sequential semantics. All these methods would need to be added to the Promise prototype as well.

I will also want to use Q.fcall to dispatch messages to promised callbacks.

abrkn commented Mar 14, 2013

I'm fine with using something on top of Q to keep the core tight. Speaking of which, why does Q.spread exists? Will definitely check out Qx! LINQ is amazing.

Owner

kriskowal commented Jun 30, 2013

I’m declining. I agree that it’s useful, but not useful enough to expand the surface of the Q core library. The case it simplifies can be express fairly succinctly in terms of primitives.

promises.invoke("map", callback, thisp).all()

spread exists perhaps because I am not entirely consistent in my verdicts, but I think that manual array destucturing is a pretty painful and common problem. It certainly will be deprecated when ES6 becomes common.

kriskowal closed this Jun 30, 2013

Owner

kriskowal commented Jul 1, 2013

I am reopening. I have reconsidered.

kriskowal reopened this Jul 1, 2013

Owner

kriskowal commented Jul 1, 2013

I am going to add map, reduce, and forEach. The semantics of map will be slightly different—the all to the result will not be implied. The promises could be aggregated in opportunistic order using reduce, which will be implemented similarly to all.

@kriskowal kriskowal commented on the diff Jul 1, 2013

@@ -847,6 +847,7 @@ function when(value, fulfilled, rejected, progressed) {
}
function _rejected(exception) {
+ console.log('rejected ' + exception.message)
@kriskowal

kriskowal Jul 1, 2013

Owner

I am removing this line.

@kriskowal kriskowal commented on the diff Jul 1, 2013

@@ -923,6 +924,13 @@ function spread(promise, fulfilled, rejected) {
}, rejected);
}
+Q.map = map;
+function map(promise, callback, fulfilled, rejected) {
+ return when(promise, function (valuesOrPromises) {
+ return all(valuesOrPromises.map(callback));
@kriskowal

kriskowal Jul 1, 2013

Owner

I am expanding this to use our array_reduce shim so that it will work in older IE’s.

@kriskowal kriskowal commented on the diff Jul 1, 2013

spec/q-spec.js
@@ -1029,6 +1029,17 @@ describe("all", function () {
return Q.all([]);
});
+ it("fails if any of the promises fail", function() {
@kriskowal

kriskowal Jul 1, 2013

Owner

This spec is invalid and fails. Q.all is not obliged to call the contained functions. However, you could Q.map(functions, Q.fcall).

kriskowal referenced this pull request Jul 1, 2013

Closed

Promise streams #334

Owner

kriskowal commented Jul 3, 2013

Subsumed in #337

kriskowal closed this Jul 3, 2013

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