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

187
Views
Javascript I cannot stop the execution of a Promise

I am runnig the code below in a .ts file in the Next API Folder. I am trying to wait the result of a call to an unofficial API of Reverso (https://www.npmjs.com/package/reverso-api). The problem is that the code below continues to wait the request even if the the response from reverso has arrived and the value has been updated. How can I can I do? The reverso.getContext('Dog', 'English', 'German', ... ) method returns a Promise.



type Data = {
   responseWord : string
}

let r = 3;

export default async function handler( req: NextApiRequest, res: NextApiResponse<Data>) {

   const Reverso = require('reverso-api');
   const reverso = new Reverso();

   console.log("1");
   await reverso.getContext('Dog', 'English', 'German', (response : any) => {
       r = response?.text;
       console.log(r);
       return;
   }).catch((err : any) => {

   });
   console.log("2");

   res.status(200).json({ responseWord : `${r}` });
}

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

reverso.getContext() can be used in both ways, by using the callback based approach, or using promises, since it returns a promise. The correct way to retrieve the response data using an async function is this:

const Reverso = require('reverso-api');
const reverso = new Reverso();

export default async function handler( req: NextApiRequest, res: NextApiResponse<Data>) {
  try {
   const { text } = await reverso.getContext('Dog', 'English', 'German');
   res.status(200).json({ responseWord : `${text}` });
  } catch(e){
      console.log(e)
  }
}

The callback based version:

const Reverso = require('reverso-api');
const reverso = new Reverso();

export default function handler( req: NextApiRequest, res: NextApiResponse<Data>) : void  {
 reverso.getContext('Dog', 'English', 'German', (response : any) => {
     res.status(200).json({ responseWord : response?.text });
   }).catch((err : any) => {
     console.log(err)
   });
}
about 4 years ago · Santiago Gelvez 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!