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

292
Views
How to merge 2 serializable functions into 1 serializable function in JS?

As you know, there are certain cases (e.g., use in workers) when functions need to be serializable (and, at the other side, deserializable of course). I have such a case; a library function (i.e. I cannot change it) doSomething(serializableFunction) expects such a function from me. Also it expects that serializableFunction accepts 1 parameter x and gives x to it.

So far, this was not a problem for me; I had one function f(x) from another library that fulfilled my purposes (so I just called doSomething(f) so far). But now, I have a second serializable function g(x), also from a library, and I want the function I give to doSomething to execute either f or g, dependent on x. So I tried:

doSomething(function h(x) {
  if (x < 5) return f(x);
  else return g(x);
});

This would work well without serialization, but with it, an error is thrown of course because h is not serializable anymore: It calls f and g, which are no longer known at deserialization time!

So currently I'm doing:

doSomething(eval(`function h(x) {
  const f = ${f.toString()};
  const g = ${g.toString()};
  if (x < 5) return f(x);
  else return g(x);
}`));

This works, but it's a nightmare! So my question is: Is there any better approach to merge 2 serializable functions (like fand g) into 1 function that is also serializable? Note that I cannot change any of f, g, or doSomething, just h!

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

0

I think of a solution replacing eval with new Funciton. Not the best, but a bit better.

Example below:

function f(a) {
    console.log("f", a);
}

function g(b) {
    console.log("f", b);
}

function doSomething(func) {
    const y = 1;
    func(y);
}

const funcBody = `return function () {
    const f = ${f.toString()};
    const g = ${g.toString()};
    if (x < 5) return f(x);
    else return g(x);
  }()`;

const func = new Function("x", funcBody);

doSomething(func);

Or you can add some abstraction like below:

function f(a) {
    console.log("f", a);
}

function g(b) {
    console.log("f", b);
}

function doSomething(func) {
    const y = 1;
    func(y);
}

const getFuncArray = (name = "x") => [
    name,
    `return function () {
        const f = ${f.toString()};
        const g = ${g.toString()};
        if (${name} < 5) return f(${name});
        else return g(${name});
    }()`,
];

const res = new Function(...getFuncArray());

doSomething(res);

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!