Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
my object is modified inside a function that has a promise even if function returned

i have the following function that should return a result object

function checkRegisteredStatus(addr)
{
  let result ={status:"",registrationType:""};
  instance.methods.isRegistered(session.id,addr).call().then((receipt)=>
  {
    let {status,registrationType}= receipt;  
    result["status"] = status;
    result["registrationType"]= registrationType;
  });
  return result;
}

i want to use this function like this:

let result = checkRegisteredStatus(addr)
  • i have three problems :

1- when i used async/await pattern on isRegistered() method like this:

    let result = await instance.methods.isRegistered(session.id,addr).call();
    return result;

result object would be a promise with unfulfilled status, which is wired since the purpose of await is to return the result of the resolve callback, and this is the first time that happens to me, i did async/await before and it always returns the final result not a promise. why is this happening?

2- due to the first problem i had to re-write my function the old way which is using.then() method (as i pasted above) and it works, however i dont understand how come the checkRegisteredStatus function should finish execution and is popped off from the call stack then how is it being able to modify the result object?

Example:
let result = checkRegisteredStatus(addr)
console.log(result)

the output would be:

> {status:"",registration:""}

when i click the expand sign > it outputs the following:

 > {status:"",registration:""}
      registrationType: "NotRegistered"
      status: false

as far as i understand the value was caught be console.log() when the result object still had empty properties that's why i could expand the object and see its props have different values,i think JS is adding elements from inside that promise that registered before, but the function result obj should be cleaned from the stack (since it returns immediately), how is it adding values to it even though the function is popped from the call-stack?

3- how should i rewrite my function such that it blocks execution until it returns the final result not a promise and not adding up values later.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda