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

282
Vistas
Javascript - how to compose asynchronous functions

I have this code:

const compose2 = (f, g) => (...args) => f(g(...args))
const compose = (...fns) => fns.reduce(compose2)

const count = arr => arr.length
const split = str => str.split(/\s+/)
const addAsyncWord = async str => `${str} some words obtained asynchronously`
const read = text => text


const word = compose(
        count,
        split,
        (await addAsyncWord),
        read
    )

console.log('word ->', await word()) //<- edited

but is throwing the following error:

Uncaught SyntaxError: missing ) in parenthetical

output expected after reading, concat words (obtained from a server randomly, needs to be asynchronously), spliting words, and finally count the total length, must be something similar to this:

word -> 10

How to make this to work? thanks

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

0

I will post my solution here if helps anyone in the future:

const compose2 = (f, g) => async (...args) => f(await g(...args))
const compose = (...fns) => fns.reduce(compose2)

const count = arr => arr.length
const split = str => str.split(/\s+/)
const addAsyncWord = async str => `${str} some words obtained asynchronously`
const read = text => text

const word = compose(
    count,
    split,
    addAsyncWord,
    read
)

const test = async () => {
    const word1 = await word('Hello world')
    console.log('word ->', word1)
}

test()

thank you for your comments.

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