How to make async code into sync inside the async.

2019-08-26 17:53发布

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条回答
冷血范
2楼-- · 2019-08-26 18:30

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?

查看更多
登录 后发表回答