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]);
            }
        );
}
6
  • Why do you need promises here? Is anything in your code async? Commented Sep 25, 2018 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. Commented Sep 25, 2018 at 22:21
  • @vlaz, yes later I want to use async in accessing the results Commented Sep 25, 2018 at 22:22
  • 1
    @ChrisG, yes I am going to use async in later point of time to access these methods. Commented Sep 25, 2018 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. Also this stack post Commented Sep 25, 2018 at 22:24

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@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.
I guess the comment I replied is now gone =)

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.