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

94
Views
Valor de retorno de Javascript de la recursividad en la función asíncrona

Hola, hola, esta es mi función recursiva que valida las propiedades anidadas. Así que estoy tratando de devolver una matriz con todos los errores de esta función. Claramente estoy haciendo algo mal ya que hay muchos bucles. Por favor, ayúdame a devolver una matriz de objetos.

 export async function validateIt<T>(payload: T, validations: IValidation<T>[], errors = []) { return await Promise.all( validations.map(async x => { return await Promise.all(x.validators?.map(async validator => { if (!x.nestedValidations) { const results = await validate(validator, payload, x.field) errors.push(results) return errors } else { await Promise.all( payload[x.field].map(async (nestedPayloadItem) => { await validateIt(nestedPayloadItem, x.nestedValidations) }) ) } })) } ) ) }

así es como se ve la carga útil

 { "event": 103, "itemCode": "Test", "storeProductAvailabilityList": [ {"startTime": 200, "endTime": 100, "pricingType": "SELLD"}, {"type": "REG", "endTime": 100} ], "storeProductImageList": [ {"type": "ONSITE", "base64": "3123123123"}, {"type": "CHART"} ] }

Y aquí está la función de llamada

 const errors = await validateIt<PostStoreProductPayload>( payload, [{ field: "eventId", validators: [{ type: 'required' }] },{ field: "name", validators: [{ type: 'required' }] },{ field: "storeProductImageList", validators: [{ type: 'required' }], nestedValidations: [{ field: 'base64', validators: [{ type: 'required'}]}, { field: 'type', validators: [{ type: 'enum', values: ['CHART', 'CART']}]} ] }, { field: "sku", validators: [{ type: 'required' }] }] )

Obteniendo una matriz multidimensional, entonces mi pregunta es ¿esta es la forma correcta de escribir la recursividad? ¿Estoy haciendo bien el retorno de esta parte?

 if (!x.nestedValidations) { const results = await validate(validator, payload, x.field) errors.push(results) return errors }

Estoy un poco confundido con todos estos "async".

Buscando este resultado.

 [ { name: 'required' }, { sku: 'required' }, ]
about 4 years ago · Juan Pablo Isaza
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!