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

267
Views
How to call HttpException in setTimeout, when i wait promise a long time?

I get products from different servers, I want return exception, when wait a long time. I have created setTimeout for this, but it stoped server and didn't return error.

How to fix it?

      const server = 23;

      const productsTimeout = setTimeout(() => { throw new HttpException('Problem', HttpStatus.INTERNAL_SERVER_ERROR) }, 3000);

      products = await new Promise((resolve, reject) => {
        this.socketClientCabinet.on('products_get', async ({ server, products }) => {
          if (server === serverID) {
            const {
              productsSymbols,
            } = this.productsTransform(products);

            clearTimeout(productsTimeout);

            resolve(productsSymbols);
          }
        });
      });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

User delete his answer, I have fixed it.

      let productsTimeout;

      const timeoutPromise = new Promise((res, rej) => {
        instrumentsTimeout = setTimeout(() => rej(new HttpException('problem', 500)), 1000, )
      })

      const productsPromise = new Promise((resolve, reject) => {
        this.socketClientCabinet.on('products_get', async ({ server, products }) => {
          if (server === serverID) {
            const {
              productsSymbols,
            } = this.productsTransform(products);

            clearTimeout(productsTimeout);

            resolve(productsSymbols);
          }
        });
      });

      const products = await Promise.race([
        timeoutPromise,
        productsPromise
      ])

It is work.

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!