-1

After reading http://www.promisejs.org/patterns, I saw this Javascript ECMA 6.0 pattern.

function all(promises) {
    var accumulator = [];
    var ready = Promise.resolve(null);

    promises.forEach(function (promise, ndx) {
        ready = ready.then(function () {
            return promise;
        }).then(function (value) {
            accumulator[ndx] = value;
        });
    });

    return ready.then(function () { return accumulator; });
}

I am curious whether there is another way with Javascript promises to set promiseChain = promiseChain.all() to meet the objective of resolving a long chain of promises in their original sequential order. I found this StackOverflow article. http://stackoverflow.com/questions/28066429/promise-all-order-of-resolved-values which is relevant to my question. Also, could I use recursion rather than looping to allow evaluation of promises conditional on the resolution or error handling in the previous promise? Thank you.

  • Please format your code and text readably, using the preview area, **before** posting. (As this is at least your **seventh** question, you should be doing that by now.) – T.J. Crowder Feb 27 '18 at 12:01
  • I tried to format it for you, but your code is incomplete, and so I can't format it reasonably. – T.J. Crowder Feb 27 '18 at 12:01
  • When you were asking your question, there was a big orange **How to Format** box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a **[?]** button giving formatting help. *And* a preview area located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Feb 27 '18 at 12:02
  • @T.J.Crowder, I just formatted the Javascript code in line with your request. Thank you. – Francis Tuan Feb 27 '18 at 12:54

1 Answers1

1

At http://www.promisejs.org/patterns I saw this Javascript ECMA 6.0 pattern

No, it's not a pattern at all. It was meant to serve as an explanation of how Promise.all works, suggesting it could be implemented like that function all(promises) { … }. Only that this implementation is absolutely horrible, inelegant, and fails too meet the specification requirements in many ways.

The page puts it as "it should give you an idea of how promises can be combined in interesting ways." Yeah, interesting maybe, but it's really just incorrect code that should not be taken up by anyone as a pattern.

I am curious whether there is another way to meet the objective of resolving a long chain of promises in their original sequential order.

That makes no sense. A promise chain (a promise built from many successive then calls) is already implicitly sequenced, you don't have to do anything special for that.
If you are talking about an array of promises (similar to how Promise.all takes one), multiple arbitrary promises are independent from each other and do not form a sequence - nor can they be forced to do anything in sequence. Remember that a promise is the result of a running asynchronous task, it's not a task that can do anything.

Could I use recursion rather than looping to allow evaluation of promises conditional on the resolution or error handling in the previous promise?

Yes, a recursive approach goes very well with promises and is in fact the only solution to unbounded conditional repetition.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I was trying to understand the Javascript statement ready = ready.then() which uses a return by reference. Thanks. – Francis Tuan Feb 27 '18 at 13:27
  • @FrancisTuan It just builds a chain `const ready = Promise.resolve().then(…).then(…). … .then(…)` of arbitrary length in a loop. You understand what `then` does, right? – Bergi Feb 27 '18 at 13:30
  • @Bergi, A very senior software engineer just told me that there is no guarantee of the order of resolution of JavaScript promises for the parallel execution use case but it is possible to guarantee the order of resolution of the individual components of a sequential JavaScript promise chain. Thank you. – Frank Feb 27 '18 at 19:04
  • @Frank Yes, that doesn't contradict anything in my answer, does it? – Bergi Feb 27 '18 at 19:09