Closed joge17 closed 5 years ago
I am trying to return the translated word from the function translateWord. When I do console.log(val) inside of then() I get the correct translated word. But when I do return val; I get undefined.
console.log(val)
then()
return val;
undefined
This logs the translated word to the console:
translateWord(word) { let promise = translate(word, 'es'); promise.then((val) => { console.log(val); }) }
This returns undefined:
translateWord(word) { let promise = translate(word, 'es'); promise.then((val) => { return val; }) }
How can I return the translated word from the translateWord function?
translateWord
Since it is an asynchronous operation, you cannot return the plain value. With promises you'd have to use await. Please read this amazing article:
await
I am trying to return the translated word from the function translateWord. When I do
console.log(val)
inside ofthen()
I get the correct translated word. But when I doreturn val;
I getundefined
.This logs the translated word to the console:
This returns
undefined
:How can I return the translated word from the
translateWord
function?