Home>
I want to achieve

The corresponding source code is processed in parallel as follows.

  Promise.all ([[
      wrap (func1), // I want to pass arguments
      wrap (func2)
      ])


Argument to this'apple'I want to pass and do the following, but I get an error.

  Promise.all ([[
      wrap (func1 ('apple')), // error
      wrap (func2)
      ])
Corresponding source code

The 11th line below is the argument'apple'This is the line I want to pass.

Run the source code

// While processing func1 and func2 in parallel, the results will be processed sequentially.
$(function () {
    // Results are processed sequentially
    const wrap = async function (func) {
      const res = await func ();
      console.log (res);
      return res;
    }
    // Process func1 and func2 in parallel
    console.log ("Start parallel processing");
    Promise.all ([[
      wrap (func1), // I want to pass arguments
      wrap (func2)
    ]) .then (res =>{
        console.log (res);
        console.log ("All parallel processing finished");
    });
    // Get data with AJAX
    function func1 (str ='') {
      var def = new $.Deferred ();
      console.log ('start func1')
      setTimeout (function () {
          const ajaxResponse1 = [{name: str +'Taro'}, {nane: str +'Jiro'}];
          def.resolve (ajaxResponse1);
      }, 1000);
      return def.promise ();
    }
    // Get data with AJAX
    function func2 () {
      var def = new $.Deferred ();
      console.log ('start func2');
      setTimeout (function () {
          const ajaxResponse2 = [{name:'Hanako'}, {nane:'Ryoko'}];
          def.resolve (ajaxResponse2);
      }, 2000);
      return def.promise ();
    }
});

argument'apple'If i pass, you will get the following error:

Uncaught (in promise) TypeError: func is not a function
Consideration/Trial

I don't know if it's correct,
wrap (func1)
To
wrap (func1 ('apple'))
Thenwrapamong()Because there is a function at this pointfunc1Is it caused by the execution of? I think.

So
wrap ("func1 ('apple')")
I tried to pass it as a character string like thisevalI tried the method of processing a character string as a function in.

$(function () {
    // Results are processed sequentially
    const wrap = async function (func) {
      const res = await eval (func);// Process strings as functions with eval
      console.log (res);
      return res;
    }
    // Process func1 and func2 in parallel
    console.log ("Start parallel processing");
    Promise.all ([[
      wrap ("func1 ('apple')"), // pass as a string
      wrap ("func2 ()") // Pass as a string as well
    ]) .then (res =>{
        console.log (res);
        console.log ("All parallel processing finished");
    });
/ *-Omitted below-* /
Question

It seems that this was possible, butevalI see articles that people should avoid.

ThereforeevalArguments without using'apple'I would like to know how to pass, is it possible?

  • Answer # 1

    I'm not sure what I want to do,wrap (func1.bind (this,'apple'))Andwrap (() =>func1 ('apple'))I think it can be done by combing.

  • Answer # 2

    wrap (func1 ('apple'))
    If so, is it because the function func1 is executed at this point because there is () in the wrap? I think.

    I think you're right.

    Maybe ↓ Would you like to write like this?

    wrap (func1,'apple'),
    wrap (func2),
    wrap (func3,'banana','orange'),
    ...

    If so,
    wrap ()The function isfuncAnd any number of arguments (that is, variadic arguments) should be accepted.

    Below is a sample.

    const w = function (f, ... args) {
      // ↑ No matter how many arguments come
      // ↓ Call f with them as arguments.
      let res = f (... args);
      return res;
    }
    function f1 (s) {return'[' + s +']';}
    function f2 () {return'f2';}
    function f3 (s, t) {return s +',' + t;}
    w (f1,'apple');// =>"[apple]"
    w (f2);// =>"f2"
    w (f3,'banana','orange');// =>"banana, orange"

Related articles