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

439
Visualizações
Can someone explain pls why the Promise returns pending in the first function, but resolved in the other two?

Can someone explain pls why the Promise returns pending in the first function, but resolved in the other two? When i read MDN it states that just using the word async wont make the code inside asynchronous (we need to use the word await as well). Also, I return the promise explicitly, since I use resolve() (which should return fulfilled promise as in two other functions, yet it returns Pending in the first function). I provided the code below.

const testFunc = async() => {
  return new Promise((resolve,reject)=> {
    resolve()
  })
}

const testFunc2 = () => {
  return new Promise((resolve,reject)=> {
    resolve()
  })
}

const testFunc3 = () => {
  return new Promise((resolve,reject)=> {
    resolve()
  })
}

testFunc().then(()=> console.log("Hello")).then(()=> console.log("there")).then(()=> console.log("Obi One"))

testFunc2().then(()=> console.log(1)).then(()=> console.log(2)).then(()=> console.log(3))

testFunc3().then(()=> console.log(4)).then(()=> console.log(5)).then(()=> console.log(6))

console.log(testFunc())
console.log(testFunc2())
console.log(testFunc3())

// Results in the console: 
// Promise {<pending>}
// Promise {<fulfilled>: undefined}
// Promise {<fulfilled>: undefined}
// 1
// 4
// 2
// 5
// Hello
// 3
// 6
// there

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Thats because testFunc (the function with async keyword) is equalevent to this:

const testFunc = () => {
  return new Promise((resolve,reject)=> {
    resolve(new Promise((r) => {
      r()
    }))
  })
}

and not

const testFunc = () => {
  return new Promise((resolve,reject)=> {
    resolve()
  })
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Using async queues the executer as a microtask.

Read about it here: https://whistlr.info/2021/async-and-tasks/

The key point:

When you write an asynchronous function, you force the caller to use a microtask to read its result. This is the case even if you don't await inside it—it will return a Promise regardless

about 4 years ago · Juan Pablo Isaza Relatório
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