-1

When I run the script (see below), the first attempt fails to get the result,

the "result" variable outputs "underfined"

var result;
Promise.resolve('information')
    .then(res => {return result=res})
    result;

Attempt #1

enter image description here

On the second attempt , the values are already assigned

enter image description here

Question: How can I expect to assign a value to the "result" variable, without using setTimout and console.log

Waiting with a While loop

var result;
var finito = false;
Promise.resolve('information')
    .then(res => {return result=res})

while(finito != true)
{
  if (result != undefined)
  {
    result;
    finito=true;
  }
}

My question for Jay Surya:

This code does not work

enter image description here


My question for Azarro:

enter image description here

2 Answers2

1

Best Solution for this is to use async-await, Since you seem to need result for further operations. If you still want to use .then() approach, you will have to move your var result and all the subsequent steps which need result, into the .then()

Promise.resolve('information').then(res=>{
var result = res;
var finito=true;
//all that follows

})

But alternatively if you use async-await,


var result;
var finito = false;
result = await Promise.resolve('information');//the lines below will not be executed, until this promise resolves

while(finito != true)
{
  if (result != undefined)
  {
    result;
    finito=true;
  }
}

Ofcourse, await can only be used inside a function marked as async.

Jay Surya
  • 540
  • 1
  • 8
  • 18
1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

Promise.resolve returns a promise. If it's accompanied by a .then, it'll follow up on the .then() asynchronously (almost immediately, emphasis on almost) and resolve as a Promise.

If you want result to get the actual value then you'll have to either use await in order to force the code to act synchronously and wait on the promise's value.

async function someWrappedFunction() {
const result = await Promise.resolve('information');
}

Alternatively, you can move your code inside the .then block of Promise so that you're guaranteed a value in there.

Edit: Can you try the following:

var result;
var finito = false;
var result = await Promise.resolve('information');

while(finito != true)
{
  if (result != undefined)
  {
    result;
    finito=true;
  }
}
Azarro
  • 1,776
  • 1
  • 4
  • 11
  • Please see above i added new addition, your script does not work, please help – Csharp_cowboy May 07 '22 at 14:00
  • You forgot the `function` keyword! – MikeM May 07 '22 at 14:08
  • Whoops, thanks Mike! Hopping between languages heh – Azarro May 07 '22 at 14:09
  • @Csharp_cowboy the function keyword was missing, try it again it should work :) – Azarro May 07 '22 at 14:14
  • @Azarro, I added image question above, please take a look – Csharp_cowboy May 07 '22 at 14:17
  • @Csharp_cowboy the output is `true` because `finito=true` is the last statement that is executed, hence the output shown to you is the value of finito. If you type result at the end, you'll see the value of `result` instead – Azarro May 07 '22 at 14:18
  • @Azarro, Why its impossible to do this only with Promises without using async/await? – Csharp_cowboy May 08 '22 at 07:52
  • 1
    So if you did `var result; Promise.resolve('information').then((data) => result = data);` the reason this doesn't work is because of the infinite while loop. Promises with `thens` are guaranteed to run asynchronously; i.e. all the code after the Promise, such as the while loop, you can assume will be run first before asynchronous promises are resolved. But the promise can only execute its `then` block once the while loop has resolved. Since the while loop never yields, it'll be infinite in this case, never yielding, and thus not allowing the Promise's `then(..)` to run in the example above. – Azarro May 08 '22 at 08:00
  • 1
    That explanation is also why in your first first code block in the original question, result was initially undefined on the first attempt. It's because the `.then(...)` is executed AFTER the `result;` statement is executed because promises are asynchronous. – Azarro May 08 '22 at 08:02
  • @Azarro, could you please help me, how to output result of "b" ```function a1(callback) { var a = 2 + 2; console.log(a); callback(); } async function a2() { let b = 2 + 3; await b; } a1(a2);``` – Csharp_cowboy May 09 '22 at 06:55
  • You need to make a1 an async function as well and do `return await callback()`. Then in a2, you need to do `return await b` – Azarro May 09 '22 at 06:59
  • @Azarro, it works! thank you brother! if i want to output the result of "a" also, how can i do? ```async function a1(callback) { var a = 2 + 2; return await a; return await callback(); } async function a2() { var b = 2 + 3; return await b; } a1(a2);``` – Csharp_cowboy May 09 '22 at 07:07
  • @Azarro, please help me with this question https://stackoverflow.com/questions/72203250/javascript-how-to-execute-firstpromise-before-secondpromise/72203286#72203286 – Csharp_cowboy May 11 '22 at 15:03