Promise {<resolved>: Array(3)} __proto__: Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]: Array(3) 0: {msg1: "Hello world"} 1: {msg2: "Hello world1"} 2: {msg3: "Hello world2"} length: 3 __proto__: Array(0) 

You need to get these msg. Tried to get through the point and through square brackets, nothing happens

  • I tried as you said. Writes undefined - Chyngyz Usenov
  • I'm different. Here I am making a request. You can try - Chyngyz Usenov
  • Of course it is difficult to understand what you have there without the surrounding code. but apparently you save the promise to the variable, and you need to see the result of the promise. You have something written like the result result = myAsyncFunc (args); and it is necessary to write something like var result; myAsyncFunc (args) .then (response => result = response); - muturgan
  • I recommend to read www.stackoverflow.com/questions/554290/… - muturgan
  • Thank! Works! - Chyngyz Usenov

1 answer 1

Your object is a promise (Promise), the real value is hidden inside the [[PromiseValue]] slot. There is no access to this slot directly; you can only hook it out in two ways.

Method one is .then method:

 v.then(data => { console.log(data[0].msg1); console.log(data[1].msg2); console.log(data[2].msg3); }) 

Method two is via await inside an asynchronous method:

 data = await v; console.log(data[0].msg1); console.log(data[1].msg2); console.log(data[2].msg3); 

But do not try to "deceive" the system and pull out the value earlier than it should! A promise is an abstraction of meaning that does not yet exist, but it will be available sometime in the future. That is why there is no way to “get” the value out of it right now.

If you try to do something like this:

 let result; v.then(data => result = data); // не делайте так! console.log(result); 

then you find out that nothing happened, the result variable remained undefined.

Why it turns out you can see if you look at the order of program execution:

 let result; // 1 v.then(data => { result = data; // 4 }) // 2 console.log(result); // 3 

First, it executes 1, then it executes the then call (but the passed function has not yet been called!), Finally it executes 3, etc. And only much later, when some external event arrives, and control returns to the event queue, line 4 will be executed.