Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
JavaScript code does not output the result on the first attempt

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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;
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!