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

196
Views
la mejor manera de hacer una solicitud knex desde dentro de una promesa

Knex novato aquí, tratando de alejarse de la secuela.

Estoy tratando de usar una serie de llamadas sin formato knex en una promesa.

después de mucha investigación, se me ocurrió un ejemplo de trabajo simple, pero me gustaría que alguien con una mejor experiencia en knex/promises hiciera alguna sugerencia antes de continuar.

 const firstPromise = ( promiseInput ) => { return new Promise((resolve, reject) => { console.log('parameter passed into promise: ' + promiseInput); knex.raw("SELECT VERSION()") .then( (version) => { console.log ('inside: ' + version[0][0]["VERSION()"] ); resolve ('outside: ' + version[0][0]["VERSION()"] ); } ).catch( (err) => { reject(err); } ) }) } const secondPromise = ( input ) => { return new Promise ( (resolve, reject) => { resolve (input) ; }) } console.log('Starting run.'); firstPromise('promise input.') .then( (response) => { console.log ('response from firstPromise: ' + response) ; return response ; } ) .then( (newResponse) => console.log ('ending! ' + newResponse) ) .then( () => knex.destroy() ) .then( () => secondPromise('stuff into second promise') .then((result) => { console.log('calling second promise: ' + result); return (result);} ) ) .then( (lastResponse) => console.log ('last response: ' + lastResponse) ) .catch( (error) => console.log ('error! ' + error) ) ;

también, hágame saber si este es el foro correcto para tal pregunta.

Gracias por su tiempo y consideración.

nota al margen: es sorprendente lo fácil que es knex sobre la secuela.

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

0

No necesitas crear una nueva promesa cada vez, eso es excesivo y un poco inútil. tu puedes hacer:

 function firstPromise(promiseInput){ console.log('parameter passed into first promise: ' + promiseInput); return knex.raw("SELECT VERSION()"); } function secondPromise(promiseInput){ console.log('result of first promise passed into second promise: ' + promiseInput); return [your knex stuff here] } [... other promieses] firstPromise() .then(secondPromise) .then(thirdPromise) .then(console.log)

Todas sus promesas toman el resultado de la promesa anterior y regresan a la siguiente. Además, no necesita llamar a knex.destroy() porque cerrará la conexión.

ps sí, puedes hacer esas preguntas aquí.

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!