How to guarantee sequential order with angular http rest api in for loop?

后端 未结 2 1557
孤独总比滥情好
孤独总比滥情好 2020-12-17 03:04

I\'m trying to create a form that allows you to create multiple resources in sequential order.

Example below

Floor 1
Floor 2
Floor 3
...
Floor 9
         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 03:23

    You can use async / await for that purpose with the Promise resolve:

    for (let i = 1; i < (amount + 1); i++) {
        await new Promise(resolve => {
            newArea.name = name + ' ' + startAt
            startAt++
            this.areasService.createArea(newArea, parentId)
                .subscribe(
                    area => { 
                        this.added.emit(area);
                        resolve();
                    });
            });
    }
    

    Remember to put async before your function. See this PLUNKER DEMO.

提交回复
热议问题