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

255
Views
How to handle promises when using (call, bind, apply) with JavaScript functions?

I have a function like so:

let maybeRun = function (cb: Function, allow: boolean, ctx: object = window) {
    return function () {
        if (!allow) return;

        cb.call(ctx, ...arguments);
        // cb.apply(ctx, arguments);
    };
};

In case cb is synchronous there is no problem, but how should I handle the case of cb returns a promise (asynchronous)?

let cb = async ()=> {...};

let foo = maybeRun(cb, true);

await foo()

I want the returned function foo to have same behaviour of cb (sync or async).

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

0

You don't have to do anything special at all; an async function just returns a promise like any other return value.

But note that you're missing a return in your wrapper function. Your function was throwing away any return value it got (including a promise).

let maybeRun = function (cb: Function, allow: boolean, ctx: object = window) {
    return function () {
        if (!allow) return; // *** See note below

        return cb.call(ctx, ...arguments);
// −−−−−^^^^^^
    };
};

One wrinkle is that your wrapper now sometimes returns a promise and other times doesn't. There's nothing (standard) you can do to look at cb (without calling it) to see whether it returns a promise, so that may just be a limitation you have to deal with. (You can sniff out whether it's an async function, but you can't know if it's a non-async function that returns a promise.)


In a comment, you've said:

i didn't write a return because the wrapped functions i need this function for have no returns

That changes things for your specific use case: You'd just use await when calling the function:

let maybeRun = function (cb: Function, allow: boolean, ctx: object = window) {
    return async function () {
// −−−−−−−−^^^^^
        if (!allow) return;

        await cb.call(ctx, ...arguments);
// −−−−−^^^^^
    };
};

Now your function (being async) always returns a promise, and in the cases where cb returns a promise (either explicitly or because it's an async function), your function will wait for that promise to settle before returning, but won't return the fulfillment value.

Note that even with that change, cb is called synchronously when you call the wrapper function you got from maybeRun. An async function runs synchronously until the first time it has to wait for something. If cb is synchronous, it'll be run during that initial synchronous execution of the wrapper.

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!