1. /**
  2. * Imports data for short-lived tasks with minimal configuration.
  3. * Assumes data is in a JSON file.
  4. *
  5. * @param {string} filePath - The path to the JSON data file.
  6. * @returns {Array<object>|null} - An array of records, or null if an error occurs.
  7. */
  8. async function importShortLivedTasks(filePath) {
  9. try {
  10. // Fetch data from the specified file.
  11. const response = await fetch(filePath);
  12. if (!response.ok) {
  13. console.error(`Error fetching data from ${filePath}: ${response.status}`);
  14. return null; // Return null on error.
  15. }
  16. const data = await response.json();
  17. // Validate that the data is an array.
  18. if (!Array.isArray(data)) {
  19. console.error("Data is not an array.");
  20. return null;
  21. }
  22. return data; // Return the array of records.
  23. } catch (error) {
  24. console.error("Error importing data:", error);
  25. return null; // Return null on error.
  26. }
  27. }
  28. // Example usage (replace 'tasks.json' with your file path):
  29. // importShortLivedTasks('tasks.json')
  30. // .then(tasks => {
  31. // if (tasks) {
  32. // console.log("Tasks imported successfully:", tasks);
  33. // // Process the tasks here.
  34. // } else {
  35. // console.log("Failed to import tasks.");
  36. // }
  37. // });

Add your comment