1

I am using the viewer.getProperties(dbId, onSuccessCallback, onErrorCallback) method in order to get properties for objects in my viewer. I want to run the method for all selected objects, extract a subset of the properties for each object, and present the subsets in a table.

var subsets = [];
var selectFunctions = [];
handleSelection(selection, addProps, onError);

function handleSelection(selection, onSuccess, onError) {
  for (var i = 0; i < selection.length; i++)
    selectFunctions.push(_viewer.getProperties(selection[i], onSuccess, onError));
}

function addProps(data) { 
  var props = [];
  for (var prop in data.properties) {
    //Add property to props if some condition is true...
  }

  subsets.push(props);
}

Promise.all(_selectFunctions).then(function () {
  console.log("Handled all selections");
  //Add subsets to table...
}).catch(function (error) {
  console.log("ERRROR");
});

Since getProperties is running asynchronously I am not able to wait for all objects before the table is updated. The table is updated with one object at a time, and we would rather update all at once. Blocking IO is not a problem.

As the could shows I have been looking into Promise.all() from bluebird.js in order to control execution and wait for all getProperties calls to return, but so far unsuccessfully.

Regards, Torjus

0

1 Answer 1

2

This question is purely unrelated to the use of the viewer, you would need to look for some documentation on how to use Promises in order to wait for completion of multiple requests in parallel.

here is some pseudo code that may help you (ES6 syntax), I'm skipping error handling for sake of clarity:

// wrap get the async method in a promise so you can wait its completion
const getPropertiesAsync = (id) => {
   return new Promise((resolve, reject) => {

     _viewer.getProperties(id, (result) => {

        resolve(result)

      }, (error) => {

        reject(error)
      })
   })
} 

//create an array of asynchronous tasks for each component you want to get props on
const propTasks = componentIds.map((id) => {

  return getPropertiesAsync(id)
})

//promise version 
Promise.all(propTasks).then((results) => {

 //populate table with results
})

//OR async ES7 syntax
const results = await Promise.all(propTasks)

//populate table with results

Here is an article I wrote about using async/await with the viewer, but since the topic is much broader you should be able to find a lot more documentation by looking over the web by yourself:

Getting rid of JavaScript callbacks using async/await

Hope that helps

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

2 Comments

Could you accept the reply as answer or explain how it does not address your issue? Thanks
Thank you, Philippe! This worked perfectly, I thought there was something wrong with the getProperties-method, but it turns out I misunderstood the use of resolve and reject.

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.