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

294
Vistas
Svelte / Vanilla JS - do I need to use .then() on awaiting an async function?

This is probably best explained with code:

in my utils file I have created the following async function:

export async function post(fetch, url, body) {
    const res = await fetch(url, {
        method: 'POST',
        body,
        headers
    });
    return await res.json();
}

I've stripped down the function to avoid posting too much code.

Now in my .svelte component file, the function above is called as followed:

async function handleLogin() {
    const result = await post(fetch, 'https://accountingserver.dev/auth/login', {
        email: email,
        password: password
    });
    console.log(result);
}

In the above example, the result is output correctly as expected. But I have found I can also do the following and get the same outcome:

async function handleLogin() {
    post(fetch, 'https://accountingserver.dev/auth/login', {
        email: email,
        password: password
    }).then((result) => {
        console.log(result);
    });
}

So my question, is any of the two methods more valid and if so .. why?

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

0

It's a preference thing, but where possible you should lean towards using async/await. It makes your code cleaner and easier to read and also helps you avoid Promise Hell.

You should note that some environments might not support async/await as it is a newer feature, but most modern environments will likely natively support it. In cases where async/await is not supported, async function name(){} syntax will not be allowed and you will need to use .then() instead of await.

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