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

155
Views
Simplificar la función fp-ts

tengo esta funcion:

 export const getProducts = makeRequest( /** Your query */ `asasd*[_type == "product"] { _id, title }`, /** Optionally transform the response */ (i) => i, /** Validate exact type */ D.array(Product) );

que es de tipo:

 const getProducts: TaskEither<Error, { _id: string; title: string; }[]>

Esto se usa en una ruta API Next.js como esta:

 export default async ( _: NextApiRequest, res: NextApiResponse<Array<Product>> ) => { const request = await getProducts(); return pipe( request, E.fold((v) => res.status(400).end(v.message), res.status(200).json) ); };

Funciona, pero ¿cómo puedo simplificar la última función? Me gustaría que await getProducts() esté en línea en la canalización, por lo que no tengo que hacer la asignación de la request por adelantado.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar el map desde el módulo de Task :

 import * as T from 'fp-ts/Task' export default async ( _: NextApiRequest, res: NextApiResponse<Array<Product>>; ) => pipe( request, T.map(E.fold((v) => res.status(400).end(v.message), res.status(200).json)) )();

pipe(request, T.map(...))) devuelve un Task , por lo que debe llamarlo al final para que se ejecute.

Si realmente quisiera evitar el () al final, podría hacer algo como esto:

 const runTask = async <A>(task: T.Task<A>): Promise<A> => task(); export default async ( _: NextApiRequest, res: NextApiResponse<Array<Product>>; ) => pipe( request, T.map(E.fold((v) => res.status(400).end(v.message), res.status(200).json)), runTask );
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!