0

I'm new to Promises and currently I have an object that looks like this:

Animals{food:{...}, recreation:{...}, toys:{...}}

Currently to access the attributes I'm running Animals.food, Animals.recreation ect. We recently want to change this to an async call and the recommendation was to use a Promise. So I did the following:

function getAnimalFood(){
    return Promise.resolve(this.food)
}

function getFood(id){
   const allFood = this.getAnimalFood();
   return allFood[id]; 
}

My question is how do I access the actual promise object from food that has the data? enter image description here

lost9123193
  • 10,460
  • 26
  • 73
  • 113
  • You ***must*** go through `.then()` when dealing with promises. Async is async all the way down. – zero298 Feb 03 '21 at 20:02
  • Your code has several syntax errors that you should correct. – Randy Casburn Feb 03 '21 at 20:04
  • "*We recently want to change this to an async call*" - what? why? No, there's nothing asynchronous going on in there, so you should not use promises. Using promises doesn't make anything asynchronous. – Bergi Feb 03 '21 at 21:05

1 Answers1

1
this.getAnimalFood().then(food => {
  console.log(food)
})

The Promise's "callback" params can be retrieved from the .then() for resolve() and from the .catch from the reject()

Emmanuel
  • 161
  • 5