简体   繁体   中英

How to pass parameters to function without calling function?

I'm trying to find a way to get a function requiring a parameter without actually calling the function.

Current broken code example:

const _validations = {
  userID: (req) => check('userID').isString().isUUID('4').run(req),
};

async function (req, res, next) {
  const allReqFields = { ...req.body, ...req.params, ...req.query };

  const fieldsFound = [];
  for (let key of Object.keys(allReqFields)) {
    if (_.has(_validations, key)) {
      fieldsFound.push(_validations[key](req));
    }
  }

  await Promise.all(fieldsFound);

  return next();
},
  function (req, res, next) {
    const errors = validationResult(req);

    if (errors.isEmpty()) {
//ALWAYS HITS HERE
      return next();
    }else{}
}

Code example that works but I don't want:

const _validations = {
  userID: (req) => check('userID').isString().isUUID('4').run(req),
};

async function (req, res, next) {
  const allReqFields = { ...req.body, ...req.params, ...req.query };

  for (let key of Object.keys(allReqFields)) {
    if (_.has(_validations, key)) {
      await _validations[key](req);
    }
  }


  return next();
},
  function (req, res, next) {
    const errors = validationResult(req);

    if (errors.isEmpty()) {
//Correctly hits here
      return next();
    }else{
//Correctly hits here
    }
}

For some context:

The function (req) => check('userID').isString().isUUID('4').run(req), returns a promise.

There are going to be more keys/values inside the _validations object.

Every function inside of _validations will require req to be passed to it.

The end goal is to run all those async functions while having req passed to them.

The issue I'm having now is that fieldsFound.push(_validations[key](req)); is pushing a promise and not the function for me to call later. Which means that fieldsFound is an array of unresolved promises

Any ideas? Thanks!

const _validations = {
  userID: () => check('userID').isString().isUUID('4'),
};

Then you push the functions without calling them:

fieldsFound.push(_validations[key]);

And then at the end just call each one with req and await the promises returned:

await Promise.all(fieldsFound.map(func => func().run(req));

You can use function currying.

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. It returns a new function that expects the next argument inline.

Transform your function to something like this.

const _validations = {
  userID: (req) => () => check('userID').isString().isUUID('4').run(req),
};

So you can invoke the function later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM