javascriptecmascript-6callback

Javascript callback for two functions


Is there a way to achieve the code bellow with Javascript (ES6)?

If yes, how can I do it? I try this example, but it didn't work.

const funcA = (callback, arg1) => {
  console.log("Print arg1: " + arg1); /* Print arg1: argument1 */
  let x = 0;
  x = callback(x, );
  return x;
}

const funcB = (x, prefix) => {
  console.log("Print prefix: " + prefix); /* Print prefix: PREFIX_ */
  x = x + 1;
  return x;
}

/* Exec function funcA */
let x = funcA(funcB( ,"PREFIX_"), "argument1");
console.log("Value of x: " + x); /* Value of x: 1 */

Solution

  • Partial application is not yet possible in js. You need another arrow function that acts as a callback:

    funcA(x => funcB(x ,"PREFIX_"), "argument1");
    

    To call that you don't need that extra comma:

    x = callback(x)
    

    Somewhen this proposal might allow to write this:

     funcA( funcB(?, "PREFIX_"), "argument1")