douglascrockford / parseq

Better living thru immediacy!
214 stars 28 forks source link

Empty requestor arrays #9

Closed diachedelic closed 4 years ago

diachedelic commented 4 years ago

The documentation does not specify that the requestor arrays cannot be empty, yet when they are parseq fails with parseq.parallel: Missing requestor array..

It would be less surprising if parseq allowed empty arrays, for situations where the requestor arrays are being generated on the fly, for instance:

parseq.sequence([
    getJobs(),
    function(callback, jobs) {
        return parseq.parallel(
            // one requestor per job
            jobs.map(function(job) {
                return processJob(job); // returns a requestor
            });
        })(callback);
    },
])

The above code worked flawlessly until an edge case occurred and I received an empty jobs array, which was not an error.

douglascrockford commented 4 years ago

You are right. The documentation should be clear that a requestor array not be empty.

But you are arguing that you should not get a warning at all. Are you suggesting that an empty requestor array be considered an immediate success? If so, is the callback called immediately or in a later turn?

I assumed that an empty array is clearly a mistake. How should we distinguish a mistake from your empty set?

shuckster commented 4 years ago

Not sure this is the right API for giving a silent "pass" for an empty-array.

That's useful in other places, of course. React comes to mind, where rendering [] is a no-op.

But I don't think it should be the responsibility of parseq to check .length for you before running jobs.

diachedelic commented 4 years ago

@douglascrockford Yes I am suggesting that an empty requestor array be considered an immediate success. I do not mind when the callback is called. Passing undefined for both requestor arrays is clearly a mistake, but passing an empty array was not a mistake in my case - at least it didn't feel like a mistake. I suppose I was expecting parseq.parallel to behave like an eventual version of [].map().

Suppose I made the mistake of passing an empty literal array. Then the call would look like parseq.parallel([]) which is a very visible mistake - does it really need to fail to alert the programmer?

But I don't think it should be the responsibility of parseq to check .length for you before running jobs.

@shuckster exactly

douglascrockford commented 4 years ago

So parallel should return an empty array. sequence and fallback should return the initial value. What should an empty race return?

diachedelic commented 4 years ago

That sounds right for parallel and sequence, but not for fallback and race. The docs specify that the latter two, if successful, transform the initial value through exactly wun requestor. This is impossible if the array is empty, so should fail.

douglascrockford commented 4 years ago

I agree. Since success is impossible, fallback and race should fail immediately.

diachedelic commented 4 years ago

Regarding the callback, the argument for calling it immediately is that it is faster. The argument for calling it on a later turn is that performance characteristics will be more consistent.

douglascrockford commented 4 years ago

Thanks.