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, solution that takes any number of promises as an array, like Promise.all() #141

Open
AlexandroPerez opened this issue Mar 19, 2018 · 0 comments

Comments

@AlexandroPerez
Copy link

So I came up with the following solution after a lot of mistakes, and a solution that didn't run in parallel,
like do promise2 only after promise1 is done.

I think it runs in parallel, since I did a console.log(i) inside the for loop, and the number of promises passed to function all() printed instantly... but I can't say I'm sure it really works in parallel.

'use strict';

function all(promisesArray) {
  return new Promise(function(fulfill, reject) {
    let size = promisesArray.length;
    let counter = 0;
    let values = [];

    for (let i = 0; i < size; i++){
      promisesArray[i].then(function(value) {
        values[i] = value;
        counter++;
        if (counter === size) fulfill(values);
      });
    }
  });
}

all([getPromise1(), getPromise2()]).then(console.log);
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

1 participant