0

I have two functions to get an item's name.

getItemName(itemId) {

    getSingleItem(itemId)
        .then(item => {
            return item.Name;
        });
}

getSingleItem(itemId) {
    return httpsRequest.createRequest(this.URL.itemList + `?Item_Id=${it}`, {}, this.requestHeaders, 'GET')
        .then(result => JSON.parse(result).Records[0]);
}

So it seems that getItemName() finishes execution before the item.Name can be returned. Is the only way to get this to return correctly to wrap it in a promise, and change the return statement to resolve(item.Name)? Or is there a cleaner way to do this? Thanks!

no_parachute44
  • 365
  • 1
  • 15
  • `getItemName() finishes execution before the item.Name can be returned ` whre are u sing getItemName? – RIYAJ KHAN Jun 13 '18 at 14:29
  • You [don't need to wrap anything in a promise](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it), you already have one - you just need to `return` the `then()` chain from `getItemName`. – Bergi Jun 13 '18 at 14:31

1 Answers1

2

You were not returning the promise from getItemName function.

getItemName(itemId) {
    return getSingleItem(itemId)
        .then(item => {
            return item.Name;
        });
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • Please discourage answering obvious duplicate questions. – 31piy Jun 13 '18 at 14:29
  • 1
    @31piy That would be a good idea, but in this case the marked duplicate is wrong. The OP just forgot to return his promise.. – Keith Jun 13 '18 at 14:57
  • @Keith -- Maybe, but one should strive to find the dupes first. In this case, answerer could have searched for a relevant post, and then would have voted for this question to be closed. Still, if you feel that this question is closed with invalid reason, you can always vote to reopen it. – 31piy Jun 13 '18 at 15:01
  • @31piy `you can always vote to reopen it.`, ok done. – Keith Jun 13 '18 at 15:03