1

My problem is I cannot construct the promisesArray.

This works perfectly but I need to construct this array on the fly.

var promisesArray=[get(url1),get(url2),get(url3)];  // url1,url2,url3 are valid urls here

var promises = Promise.all(promisesArray);

promises.then(function(results) {
 console.log('this came back: ',results);
}).catch(function(error) {
  console.log(error);
});;

function get(url) {
  // Return a new promise.
  return new Promise(function(resolve, reject) {
    // Do the usual XMLHttpRequest stuff

  });
}

I am trying to build the array promisesArray above, where each element is a promise, but every way I try I am only adding the result of calling get() and I end up with an array of pending promises

Array [ Promise { "pending" }, Promise { "pending" }, Promise { "pending" }]

When I want:

promisesArray=[get(url1),get(url2),get(url3)];

For example, I have tried:

let template=['a','b','c','d'];

  var promiseArray=[]
  template.forEach(function(pos){
      let url=lookupUrl[pos]]();  //just a function that returns a Url determined by the values of template array
          promiseArray.push(get(url));
  })

but obviously I am only returning the result of the get call here..

I tried using bind, but once again I end up with an array of executing promises...

 var promiseArray = template.map(function(pos){
   var url = lookupUrl[pos]]();
   var out= get.bind(null,url);
   return out()
 })

I don't know how to create an array of uncalled functions.

[EDIT_UPDATE] As @JaromandaX pointed out in the first comment I already had what I needed, and in fact either of the above two methods work for building the array of promises. As @Roamer-1888 also pointed out, I got side tracked into thinking that I needed an array of 'uncalled functions' which was not true.

I like the method used by @HMR in his answer so I am going to try that.

19
  • 3
    so, you want an array of promises, not an array of promises? Commented Feb 12, 2018 at 4:04
  • when/how will the get be started? I mean, you could do function get(url) { return function() { // Return a new promise. return new Promise(function(resolve, reject) { // Do the usual XMLHttpRequest stuff }); }; } ... then you have an array of functions, but how will they be called? Commented Feb 12, 2018 at 4:06
  • I'm sorry I don't understand your question. I need the array as in the first section of code that works, where I typed into my program the array of promises.. I can't build it programatically. Commented Feb 12, 2018 at 4:06
  • so, you want your array to be an array of Promises? Commented Feb 12, 2018 at 4:08
  • 1
    Promise.all does not call functions, it waits for Promises to resolve Commented Feb 12, 2018 at 4:09

2 Answers 2

0

I think you're looking for map:

Promise.all(
  urls.map(get)
)

If you have an array of template then map those to url and then url to promise:

const template=['a','b','c','d'];

Promise.all(
  template.map(//map pos to url
    pos=>lookupUrl[pos]()
  ).map(//map url to promise
    get
  )
);

If you need to throttle it somehow then have a look at this answer.

Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you build you array of functions that return the get-promise and then defer execution until you're inside promise.all?

var promisesArray=[()=>get(url1),()=>get(url2),()=>get(url3)];

promise.all(promisesArray.map((x)=>x()))

This will kick-off all of your get requests at the same time and all's promise won't resolve until they're all done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.