How to make async code into sync inside the async.parallel method - NodeJs

妖精的绣舞 提交于 2019-12-25 03:19:07

问题


My backend code looks like:

exports.getDashboard = (req, res) => {
  const dashboard = {};
  const query = {};
  let start;
  let end;
  async.parallel([
    (callback) => {
      const { role } = req.user;
      switch (role) {
        case 'Admin':
          console.log('case admin is working >> >> >> >> >> >> >> >> >> >> ');
          start = moment().startOf('day'); // set to 12:00 am today
          end = moment().endOf('day'); // set to 23:59 pm today
          query.where = {
            entryTime: {
              $gt: start,
              $lt: end,
            },
          };
          break;
        case 'Manager':
          console.log('case manager is working >> >> >> >> >> >>> >> >> >> >> >> >> >>');
          Staff.findOne({
            where: {
              loginId: req.user.id,
            },
          }).then((staffData) => {
            console.log('staffData', JSON.stringify(staffData));
            query.where = {
              entryTime: {
                $gt: start,
                $lt: end,
              },
              branchId: staffData.branchId,
            };
          });
          break;
        default:
          console.log('default case');
      }

      console.log('query >> >> >> >> >> >> >> >> >> >> >> >> >> >> >>', query);
      // query is undefined if my case is manager async call.

      Visitor.findAndCountAll(query)
        .then((data) => {
          dashboard.todayentries = data;
          callback();
        });
    },
  ], (err) => {
    if (err) return res.status(500).send(err);
    return res.status(200).send(dashboard);
  });
};

If my case is Manager query become undefined.

I already ask one question for async call, they recommended to use callbacks, its okay, but how to use nested callback inside async.parallel method?

NOTE: I dont want to erase my async.parallel method.


回答1:


I tried with another callback, Working fine.

Here what i tried:

exports.getDashboard = (req, res) => {
  const dashboard = {};
  const query = {};
  let start;
  let end;
  async.parallel([
    (callback) => {

     const cb = () => {
      Visitor.findAndCountAll(query)
        .then((data) => {
          dashboard.todayentries = data;
          callback();
        });
      }


      const { role } = req.user;
      switch (role) {
        case 'Admin':
          start = moment().startOf('day'); // set to 12:00 am today
          end = moment().endOf('day'); // set to 23:59 pm today
          query.where = {
            entryTime: {
              $gt: start,
              $lt: end,
            },
          };
          cb();
          break;
        case 'Manager':
          Staff.findOne({
            where: {
              loginId: req.user.id,
            },
          }).then((staffData) => {
            console.log('staffData', JSON.stringify(staffData));
            query.where = {
              entryTime: {
                $gt: start,
                $lt: end,
              },
              branchId: staffData.branchId,
            };
            cb(); // my custom callback
          });
          break;
        default:
          console.log('default case');
      }
    },
  ], (err) => {
    if (err) return res.status(500).send(err);
    return res.status(200).send(dashboard);
  });
};

Is this good approach to use?



来源:https://stackoverflow.com/questions/52575856/how-to-make-async-code-into-sync-inside-the-async-parallel-method-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!