1

I have a bigger decorator, but just made a toy example of where my issue is.

I'm trying to update the result from inside a decorator, but it's giving me undefined. How can I do this?

let decorator = async (dummy, method) => {
    console.log(dummy)
    await method()
}
let dummy = 'dummy'
let result
decorator(dummy, async ()=> {
    result = await queryDatabase(queryParams)
})
console.log(result) // prints undefined

UPDATE

I realised I should just await the decorator. Now it's working.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
João Otero
  • 948
  • 1
  • 15
  • 30
  • How you know `queryDatabase` does not return `undefined`? `queryParams` is undefined – Daniel W. Jan 15 '19 at 13:50
  • 1
    It is defined, but omitted here. I just realised the error: I should await the decorator! ...so stupid :-) – João Otero Jan 15 '19 at 14:01
  • Ahaha you are right. It's async, `await decorator(....` – Daniel W. Jan 15 '19 at 14:04
  • 1
    Ideally the `decorator` function should `return method()` instead of only `await`ing it. You then wouldn't have the hassle of dealing with closure variables, you could just return the `queryDatabase` result. – Bergi Jan 15 '19 at 14:12
  • @Bergi the thing is that my `dummy` will be a function which is checking on the result. Is there a better way to do it? – João Otero Jan 15 '19 at 14:32
  • 1
    @JoãoOtero Maybe you should ask a new question with your whole actual code about that. I cannot really judge from your toy example – Bergi Jan 15 '19 at 14:33

0 Answers0