Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
difficult to understand promise and asycn... ;<

I'm learning React API. Promise and async are using this. and...

I found promise, async MDN docs, but I can't understand "async in somefunction always return promise object"

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

async function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}


process().then(() => {
    console.log('end')
})

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

When the async function is called, it returns a Promise.

If the async function returns a value, the state of the Promise will be resolved with the returned value.

If the async function throws an exception or some value, the state of the Promise will be a rejected with the thrown value.

An await expression can be used inside an async function, which will suspend the execution of the async function and wait for the resolution of the Promise passed to the expression. After the resolution, it will return the resolved value and continue the execution of the async function.

about 4 years ago · Juan Pablo Isaza Denunciar

0

We have Promise and Observables to deal with the asynchronous nature of functions or app and yes Async always returns a promise while obervables can be returned by both synchronous and Asynchronous functions. A promise is followed by two keywords,

const promise = FooPromise((resolve,reject) = > {
revolve("i am resolve");
reject("i am reject");
)

after this we define what to do if our promise is resolved or rejected by these keywords respectively.

.then((resolveMessage) => {
console.log(resolveMessage);
}
.catch((catchMessage) => {
console.log(CatchMessage);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

By async return promise object it means the function now returns a promise wrapper. You can consider it as a temporary placeholder instead of the actual value sent. The promise can either be rejected or resolved and till the promise is not completed it will stay in pending state.

This allows for asynchronous operations to happen easily which might take some time and disrupt the normal execution flow.

Also, The .then statement works with promises. If you try to access without async it will be throw an error since it expects a promise object.

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}

process().then(() => {
    console.log('end')
})

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda