0

So I have a function which takes an id and returns the corresponding property map via a promise. So I made a loop and iterate my function with all my Ids but it never executes the code outside of the loop.

async getProps(dbId) {
    const properties = Viewer.properties //creates an empty map
    return new Promise((resolve, reject) => {
            let prom = new Promise((resolve, reject) => {
                this.gui.getProperties(
                    dbId,
                    args => {
                        console.log('properties', args)
                        resolve(args.properties)
                    },
                    reject
                )
            })

            prom.then(props => {
                properties.set(dbId, props)
                resolve(properties.entries())
            })
            console.log('properties', properties)
        }
    )
}

What I want is to create a map with the ID as key and the array of properties as value. This happens as intended.

This is how I use the function

async getConcreteIds() {
    let wallfloorids = this.getWallIds().concat(this.getFloorIds());
    // let concreteIds = [];
    for(let id of wallfloorids) {
        let p1 = await this.view.getProps(id);
        console.log(p1);
    }
    console.log("Hello");
}

I iterate through my IDs and call the getProperties function and it logs the map as intended. But it never leaves the loop. wallfloorids has 52 entries, so it is not an endless loop. I think the structure of my getProperties function is the cause, but I do not know what to change. I tried changing the structure, but then it returns an empty map and the rest of the code is executed. Thank you!

Daniel Ignjat
  • 73
  • 1
  • 1
  • 7
  • You're calling `this.view.getProperties` in `getConcreteIds`. Where is it defined ? Is there some confusion with `getProps` ? – Denys Séguret Jun 01 '18 at 11:51
  • My bad I changed the name while typing the question. Sloppy from my side. Editing it right now – Daniel Ignjat Jun 01 '18 at 11:53
  • 1
    Don't make getProps `async` if it explicitly returns a promise. And why are you wrapping promises in promises in getProps ? – Denys Séguret Jun 01 '18 at 11:57
  • I think it was a working remnant of a try and error process. I implemented the changes and `return new Promise((resolve, reject) => { this.gui.getProperties( dbId, args => { console.log('properties', args) resolve(args.properties) }, reject ) }) ` I basically unwrapped it and returned the single promise. It is still able to return the property, but it does not execute the code after the loop – Daniel Ignjat Jun 01 '18 at 12:21
  • 1
    It was fixed by implementing your changes and removing the await and instead using p1.then(props=>{}). Thank you very much! – Daniel Ignjat Jun 01 '18 at 12:48
  • You're welcome. I suggest you write an answer with the correct code and then accept it. – Denys Séguret Jun 01 '18 at 12:57
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Jun 01 '18 at 13:07

1 Answers1

0

Now State

getProperties(dbId) {
    const properties = Viewer.properties
    return new Promise((resolve, reject) => {
        this.gui.getProperties(
            dbId,
            args => {
                resolve(args.properties)
            },
            reject
        )
    })

}


async getConcreteIds() {
    let wallfloorids = this.getWallIds().concat(this.getFloorIds());
    let concreteIds = [];
    for (let id of wallfloorids) {
        let p1 = await this.view.getProperties(id);
        console.log(p1);
            for (let prop of p1){
                if (prop.displayCategory === "Materialien und Oberflächen" && prop.displayValue.contains("Concrete"))
                    concreteIds.push(id);
            }
    }
    return concreteIds;
}

onlyConcrete() {
    let ids = [];
    const concreteIds = this.getConcreteIds();
    console.log(concreteIds);
    this.viewer.isolateById(concreteIds)
}
Daniel Ignjat
  • 73
  • 1
  • 1
  • 7
  • Don't use `p1.then` when you can `await p1` – Bergi Jun 01 '18 at 13:08
  • I tried replacing my p1.then with await right now and it results in a forever pending Promise and my program becomes unresponsive. I will update my code above. The `p1.then` approach delivered a result, but the `this.viewer.isolateById(concreteIds)` does not wait for concreteIds to have values, which is another problem – Daniel Ignjat Jun 01 '18 at 14:19