0

I am working on a simple task where I have an array of shapes like square and rectangle and there sizes as another array passed as a parameter to function called findAllObjectArea.

I want to return a promise from my function findAllObjectArea which represents an array of areas of all my objects. Also inside my function, findObjectArea I want to return promise which represents the area.

I have created below template but I am struggling to find out the correct syntax of it. Can you please help me on this.

let findObjectArea = (myobject, sizes) => {
    new Promise(function(resolve, reject) {
        if(myobject== 'square') {
            resolve(sizes[0]*sizes[0]);
        } else if(myobject== 'rectangle') {
            resolve(sizes[0]*sizes[1]);
        }
    }
}

let findAllObjectArea = (objects, all_sizes) => {

        new Promise((resolve, reject) => {
            for(var i=0; i<objects.length; i++) {
                findObjectArea (objects[i], all_sizes[i]);
            }
        );
}
learner
  • 6,062
  • 14
  • 79
  • 139
  • Why do you need promises here? Is anything in your code async? – VLAZ Sep 25 '18 at 22:21
  • For an array of Promises you would need Promise.all(), however using a Promise here is nonsense in the first place. Promises are used for asynchronous operations like requesting data from the server. –  Sep 25 '18 at 22:21
  • @vlaz, yes later I want to use async in accessing the results – learner Sep 25 '18 at 22:22
  • 1
    @ChrisG, yes I am going to use async in later point of time to access these methods. – learner Sep 25 '18 at 22:23
  • @ChrisG Not always. Sometimes you want to do time-consuming client-side operations in parallel, in which you can fire operations off as asynchronous and use promises to handle when they are all done.[Here's a pretty decent article about that](https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need-promises-9b259854c161). Also [this stack post](https://stackoverflow.com/questions/45285129/any-difference-between-await-promise-all-and-multiple-await) – mhodges Sep 25 '18 at 22:24
  • the main issue that stands out is the fact that the promise created in `findAllObjectArea` is never resolved (or rejected) - so it out be permanently in a pending state - also, since neither of the functions return any value, none of the code makes much sense at all – Jaromanda X Sep 25 '18 at 22:53

1 Answers1

1

Rewrite of your second function:

// Use `const`, you're never gonna change this.
const findAllObjectArea = (objects, all_sizes) => {
  const promises = [];

  // I'm using 'let'. Why use 'var' if you have access to 'let' ;)
  for(let i=0; i < objects.length; i++) {
     promises.push(findObjectArea (objects[i], all_sizes[i]));
  }
  // Now we wait for ALL promises to resolve
  return Promise.all(promises);
}

It's not clear from your question what you want to do with the result of all these operations, so I'm assuming you want to return an array with all the results.

Just for fun, here's another alternative with more modern looping primitives:

// Use `const`, you're never gonna change this.
const findAllObjectArea = (objects, all_sizes) => {
  const promises = [];

  // I'm using 'let'. Why use 'var' if you have access to 'let' ;)
  for(const [index, obj] of objects.entries()) {

     promises.push(findObjectArea (obj, all_sizes[index]));

  }
  // Now we wait for ALL promises to resolve
  return Promise.all(promises);
}
Evert
  • 93,428
  • 18
  • 118
  • 189
  • @JaromandaX this is super subjective, but I personally prefer javascript primitive loops over high-level functions if they are similar in readability. The benefit is that loops also work with async/await. – Evert Sep 25 '18 at 23:01
  • I guess the comment I replied is now gone =) – Evert Sep 25 '18 at 23:16