使用回调/匿名函数进行迭代

时间:2018-04-29 05:03:47

标签: javascript schedule iife

我是Node.JS和一般的高级Javascript的新手,但是我正在尝试自己构建一个计划管理器应用程序,并且在尝试执行以下代码时遇到了一个问题(稍后我会详细说明) :

router.get('/', function (req, res) {
var day = new Date(req.query.day);
Location.getLocations(function (err, locations) {
    if (locations.length > 0) {
        var i;
        for (i = 0; i < locations.length; i++) {
            var location = locations[i];
            Appointment.getAppointments(day, location, function (err, appointments) {
                if (err) throw err;
                if (appointments.length == 0) {
                    // CREATE APPOINTMENTS
                    for (var j = location.available_time_start; j <= location.available_time_end; j += location.appointment_duration) {
                        var newAppointment = new Appointment();

                        newAppointment.start_date = new Date(day.getFullYear(), day.getMonth() + 1, day.getDate(), j);
                        newAppointment.appointment_duration = location.appointment_duration;
                        newAppointment.location = location.id;
                        newAppointment.booked = false;
                        newAppointment.locked = false;

                        Appointment.createAppointment(newAppointment, function (err, appointment) {
                            if (err) throw err;
                            console.log(appointment.location + ' - ' + appointment.start_date);
                        });
                    }
                }
            });
        }
    } else {
        // THERE ARE NO LOCATIONS
    }

    res.render('appointments', { locations: locations });
});

问题是:
当我尝试迭代locations对象然后执行getAppointments函数时,代码不会在此时执行。之后,当它被执行时,location对象总是相同的(迭代不起作用),导致意外的结果(所有约会具有相同/最后的位置)。

我尝试使用IIFE(立即调用函数表达式)立即执行代码,但是当我这样做时,我无法获得appointments回调对象,我的逻辑也被破坏了。

提前致谢!

2 个答案:

答案 0 :(得分:0)

@ JaromandaX建议使用let代替var来解决问题。

答案 1 :(得分:0)

您的代码似乎是保存约会,但对保存的约会没有任何作用(您是否在改变位置?)。

当保存约会出错时,请求者不知道它,因为createAppointment是异步的,并且回调被回调时res.render('appointments', { locations: locations });已经执行。

您可以尝试将基于回调的函数转换为承诺:

const asPromise = (fn,...args) =>
  new Promise(
    (resolve,reject)=>
      fn.apply(undefined,
        args.concat(//assuming one value to resole
          (err,result)=>(err)?reject(err):resolve(result)
        )
      )
  );
const savedAppointmentsForLocation = (day,location,appointments) => {
  const savedAppointments = [];
  if (appointments.length == 0) {
    // CREATE APPOINTMENTS
    for (var j = location.available_time_start; j <= location.available_time_end; j += location.appointment_duration) {
      var newAppointment = new Appointment();
      newAppointment.start_date = new Date(day.getFullYear(), day.getMonth() + 1, day.getDate(), j);
      newAppointment.appointment_duration = location.appointment_duration;
      newAppointment.location = location.id;
      newAppointment.booked = false;
      newAppointment.locked = false;
      savedAppointments.push(
        asPromise(
          Appointment.createAppointment.bind(Appointment),
          newAppointment
        )
      );
    }
  }
  //you are not doing anything with the result of the saved appointment
  //  I'll save it as promise to see if something went wrong to indicate
  //  to the requestor of the api that something went wrong
  return Promise.all(savedAppointments);
}

router.get('/', function (req, res) {
  var day = new Date(req.query.day);
  asPromise(Location.getLocations.bind(Location))
  .then(
    locations=>
      promise.all(
        locations.map(
          location=>
            asPromise(Appointment.getAppointments.bind(Appointment),[day,location])
            .then(appointments=>[location,appointments])
        )
      )
  )
  .then(
    results=>//results should be [ [location,[appointment,appointment]],...]
      Promise.all(
        results.map(
          ([location,appointments])=>
            savedAppointmentsForLocation(day,location,appointments)
            .then(ignoredSavedAppointment=>location)
        )
      )
  )
  .then(locations=>res.render('appointments', { locations: locations }))
  .catch(
    error=>{
      console.log("something went wrong:",error);
      res.status(500).send("Error in code");
    }
  )
});