G
G
game8022017-10-14 21:32:20
JavaScript
game802, 2017-10-14 21:32:20

How to return the result of a promise to a variable?

Good afternoon / evening, dear connoisseurs. I ran into a problem that I can’t solve, maybe someone will give good advice (if possible, describe the mechanism of work, please).
I have the following code:

var p1 = translate('Привет', {from: 'ru', to: lang}).then( function(res) {
    return res.text
});

console.log(p1);

In the end, I get back Promise { <pending> }. How can I rewrite this code in such a way that I get the value returned to res.textthe variable p1?
Thanks...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2017-10-14
@game802

You're trying to use the result of an asynchronous function in synchronous code, and you can't (and can't) do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question