Search code examples
javascriptes6-promise

No code gets executed after loop with promises


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!


Solution

  • 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)
    }