Search code examples
javascriptnode.jses6-promise

UnhandledPromiseRejectionWarning: Unhandled promise rejection - Node.js


I have separated the validation module in a async function. But it is not throwing destined error instead it is giving UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

Here is the validation function :

let pickupDateAndTimeValidation = async (order) => {

  try {
    const isPickupToday = order.pickupDate === order.orderDate
    order.orderTime = helper.convertTime12to24(order.orderTime);
    const pickupDate = helper.dateFromMMDDYYYYString(order.pickupDate)
    order.pickupDate = pickupDate.toISOString().split('T')[0]
    order.pickupSlotStartTime = helper.convertTime12to24(order.pickupSlotStartTime)
    order.pickupSlotEndTime = helper.convertTime12to24(order.pickupSlotEndTime)

    const pickupSlotStartTime = helper.dateFromTimeString(order.pickupSlotStartTime, pickupDate)

    const firstAvailableSlot = isPickupToday
      ? await getFirstAvailableSlotAfterTime(order.storeID, helper.getDayOfWeek(pickupDate), order.orderTime)
      : await getFirstAvailableSlotOfDay(order.storeID, helper.getDayOfWeek(pickupDate))
    if (!firstAvailableSlot)
      throw helper.conflict("Selected pickup slot is no longer available.")

    order.storeSlotID = firstAvailableSlot.slotID

    const orderSlotStartTime = helper.dateFromTimeString(firstAvailableSlot.slotStartTime, pickupDate)
    if (pickupSlotStartTime < orderSlotStartTime)
      throw helper.conflict("Selected pickup slot is no longer available.")
    
  } catch (error) {
    throw error
  }
    
}


And I am calling this function here :

const editOrder = async (updatedOrder, orderNumber) => {
  // if (updatedOrder.orderDate !== undefined || updatedOrder.orderTime !== undefined)
  //   throw helper.badRequest("Order date and time cannot be changed.")

  if (updatedOrder.pickupSlotStartTime)
    updatedOrder.pickupSlotStartTime = helper.convertTime12to24(updatedOrder.pickupSlotStartTime)

  if (updatedOrder.pickupSlotEndTime)
    updatedOrder.pickupSlotEndTime = helper.convertTime12to24(updatedOrder.pickupSlotEndTime)

  pickupDateAndTimeValidation(updatedOrder);

  const updateQuery = "UPDATE `transaction` SET ? WHERE orderNumber = ?;"

  try {
    await helper.runQuery(updateQuery, [updatedOrder, orderNumber])
  } catch (error) {
    if (error.errno === 1064 || error.errno === 1054)
      throw helper.badRequest()
    else
      throw error
  }

  return findOrder('orderNumber', orderNumber)
};


Any help will be appreciated why this works in editOrder function and not in separate function.. Thanks


Solution

  • pickupDateAndTimeValidation is a function that returns a promise (async functions return a promise). If the async function throws an exception, then that promise is rejected. If a promise is rejected, that rejection must be handled, else you will get that UnhandledPromiseRejection error.

    So the problem in your code is that your promise-returning function is throwing an exception, but you aren't handling it. Look at how the validation function is called:

    const editOrder = async (updatedOrder, orderNumber) => {
      // ...
    
      pickupDateAndTimeValidation(updatedOrder);
    
      // ...
    };
    

    That's it. You've invoked the promise-returning function, but you haven't set any sort of error handling around it. So either use promise notation to handle errors e.g. pickupDateAndTimeValidation(updatedOrder).catch(err => ...) or, since this is an async function, use await on it within a try/catch block to catch any error thrown by that function.