I am new to node js and I have a function where I should pass the source and destinations and then the result will be distance among the given places: here is a sample of my code :
    cartSchema.methods.getLocation = function (destinations, origins) {
  let result = {};
  try {
    distance.matrix(origins, destinations, function (err, distances) {
      if (!err) result = distances;
    });
  } catch (e) {
    res.status(500).json({ message: e.message });
  }
  console.log(result);
  return result;
};
as you can see here I want to return the distances where I have passed its value to the result but it still not return undefined.
Thanks in advance
 
     
    