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

237
Views
is it ok to pass async function where non async function needed?

as below, an async function was given for a paramter where non-async function expected, but it worked, why?


function getCallback(param, callback) {
  setTimeout(() => {
    callback && callback(param);
  }, 1);
}

getCallback(13, async (param) => {
  console.log(param);
});

---
13

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

0

It worked, why?

Because an async function is just a function that returns a promise and can be called like any other function. Your getCallback ignores the return value of callback(), so it doesn't matter what it returns.

Is it ok to pass async function where no async function is needed?

No, it generally is not ok to pass an async function (or another function that returns a promise) to a function that doesn't expect the callback to return a promise. Promises should not be ignored, they should be .then()-chained or awaited, and they need special error handling. If the getCallback does not handle (promise) errors, you must not pass a callback that errors.

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!