Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!