0

I have a function which takes some time to complete, because of that , I want to turn it into a promise.

  • My goal is to be able to call the function/promise and after it is fullfilled I can use use a .then() to do whatever I want.

  • I want getPlantMaterials() to be resolved , after I get the API response.

I'am not sure how to correctly convert this into a promise, though :

getPlantMaterials() {
            this.isLoadingPlantMaterials = true;
            this.$apollo
                .query({
                    query: gql`
                        query getMaterialByPotSizeId($plantPotSizeId: ID) {
                            tenantPlantMaterials(
                                plantPotSize_Id: $plantPotSizeId
                            ) {
                                edges {
                                    node {
                                        id
                                        quantity
                                        material {
                                            id
                                            name
                                            materialType {
                                                name
                                                id
                                            }
                                            brand
                                            vendor
                                            size
                                            unit
                                            inventory
                                            id
                                        }
                                    }
                                }
                                totalCount
                            }
                        }
                    `,
                    variables: {
                        plantPotSizeId: this.selectedPlant.plantPotSize.id,
                        materialId: null,
                        quantity: null
                    }
                })
                .then(response => {
                    this.selectedPlantMaterials =
                        response.data.tenantPlantMaterials.edges;
                    this.selectedPlantMaterials.map(item => {
                        item.node.totalAmount =
                            item.node.quantity * this.totalPlantQuantity;
                    });
                    console.log(
                        "this.selectedPlantMaterials",
                        this.selectedPlantMaterials
                    );
                })
                .finally(() => {
                    this.isLoadingPlantMaterials = false;
                });
        },

Simao
  • 371
  • 2
  • 13
  • 2
    it already has a promise - see the `.then` and `.finally` – Daniel A. White May 29 '22 at 15:09
  • yes, I know , but I want the function ```getPlantMaterials```` to be a promise itself. It is possible, right ? – Simao May 29 '22 at 15:12
  • 1
    You can read more [here](https://stackoverflow.com/questions/39458201/understanding-javascript-promise-object) on how it works and you can create your own Promise – Idrizi.A May 29 '22 at 15:13
  • 1
    Then just `return` that promise chain you've created? – Bergi May 29 '22 at 16:44
  • @Enve Are you suggesting to create a `new Promise`? – Bergi May 29 '22 at 16:45
  • @Bergi, Yup , I think he is. Something like ```getPlantMaterials = new Promise(res, rej)``` – Simao May 29 '22 at 17:32
  • 2
    @Simao Then no, don't do that. It would be the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it). Just place a `return` keyword in your current code and it'll work. – Bergi May 29 '22 at 17:35

0 Answers0