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

141
Views
How to return a value within a function with an event listener?

I have a function like so:

async function websocketRequest() {
  ws.onmessage(message => {
     // do some validation with message
     // how to return message here?
  })
} 

What I'm trying to accomplish is make a function that sends a websocket request to the server with a unique request id, when the server responds, it should use the request id to match the response with the request.

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

0

Maybe wrapping your function in a promise and resolving the message would fit your goal?

async function websocketRequest() {
  return await new Promise(res => {
    ws.onmessage(message => {
      //do some validation
      res(message);
    });
  });
}
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!