Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 11, shorter solution by avoiding duplicated code #97

Open
mnummeli opened this issue Sep 9, 2016 · 2 comments
Open

Exercise 11, shorter solution by avoiding duplicated code #97

mnummeli opened this issue Sep 9, 2016 · 2 comments

Comments

@mnummeli
Copy link

mnummeli commented Sep 9, 2016

It is possible to write an inner function to handle the otherwise duplicate code in promise handlers, i.e.

'use strict';

function all(promise1, promise2) {
    return new Promise((fulfill, reject) => {
    let counter = 0;
    let out = [];
    function update(val, loc) {
        out[loc] = val;
        counter++;
        if(counter >= 2) {
        fulfill(out);
        }
    }
    promise1.then((val) => update(val, 0));
    promise2.then((val) => update(val, 1));
    });
}

all(getPromise1(), getPromise2()).then(console.log);
@ayhtrak
Copy link

ayhtrak commented Jan 16, 2017

Seriously??

The whole point of the solution was to make clear the synchronous behavior of Promise.all
Abstracting the multiple call by wrapping in a custom function doesn't serve the purpose

@ohcibi
Copy link

ohcibi commented Jun 17, 2017

@ayhtrak in what way is the official solution making clear that Promise.all is synchronous?? The current solution as well as this proposed solutions both do two sequentially calls to .then; the only difference is that this solution has the function passed to .then declared before both calls..

If you really want to show the synchronous behavior, the solution should go like

promise1.then(() => {
   // ...
   return promise2
}).then(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants