Javascript callback for two functions

2019-06-02 11:09发布

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 */

3条回答
戒情不戒烟
2楼-- · 2019-06-02 11:34

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")
查看更多
混吃等死
3楼-- · 2019-06-02 11:43

One approach would be to define a default parameter that is a function for the first parameter passed to funcA and use var to define x when funcA is called

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

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

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

查看更多
Explosion°爆炸
4楼-- · 2019-06-02 11:47

This is an approach with a defined placeholder as symbol to identify the parameter which is not yet set.

It features a this object which is bind to the calling function for further check and evaluation.

If the combined array of arguments object and this.arg has no more placeholder items, the function is called with parameters and return the function call.

If not, the new arguments array is bind to the function and returnd.

[?] denotes the placeholder symbol

 funcB    x    prefix       this.args        args       action
-------  ---  ---------  -------------  --------------  ------------------------------
1. call  [?]  "PREFIX_"                 [?], "PREFIX_"  return calling fn w/ bound args
2. call   0      [?]     [?], "PREFIX_"  0,  "PREFIX_"  return fn call with args
3. call   0   "PREFIX_"                                 return 1

(Of course it could be a bit shorter and delegated to another function, but it's a proof of concept.)

function funcA(callback, arg1) {
    console.log('funcA', callback, arg1)
    return callback(0, placeholder);
}

function funcB(x, prefix) {
    var args = this && this.args || [],
        temp = Array.from(arguments);

    console.log('funcB', isPlaceholder(x) ? '[?]' : x, isPlaceholder(prefix) ? '[?]' : prefix);

    // placeholder part
    if (temp.some(isPlaceholder)) {
        temp.forEach((a, i) => isPlaceholder(a) && i in args || (args[i] = a));
        return args.some(isPlaceholder)
            ? funcB.bind({ args })
            : funcB(...args);
    }

    // origin function body
    return x + 1;
}

const
    placeholder = Symbol('placeholder'),
    isPlaceholder = v => v === placeholder;

console.log("Value of x: " + funcA(funcB(placeholder, "PREFIX_"), "argument1"));

查看更多
登录 后发表回答