Asynchronously flatten nested data via getBatch and getValueList
This async flatten problem relies on async functions to process each level. Given a nested JSON structure, the task is to create two utility functions and one primary function to efficiently process and flatten the structure asynchronously. The functions to be implemented are:
getBatch to fetch items within a specified range (fromIndex to toIndex). It aims to flatten these items into a single-level array of values while preserving the original order found in the nested structure.The primary challenge lies in the efficient management of asynchronous operations coupled with the maintenance of the correct order of values in the flattened structure.
Example Use Case: Consider the following input structure, which represents a simplified hierarchical data model akin to a file system or a comment thread, where each item can contain children:
[
{
"value": "value0",
"children": []
},
{
"value": "value1",
"children": [
{
"value": "value2",
"children": [
{
"value": "value3",
"children": []
}
]
},
{
"value": "value4",
"children": []
}
]
},
{
"value": "value5",
"children": []
},
{
"value": "value6",
"children": []
}
]
The objective is to flatten this structure into a single-level array of values ["value0", "value1", "value2", "value3", "value4", "value5", "value6"], using asynchronous functions to fetch and process the data efficiently.
value field and a children field, which is an array that may contain more items.getBatch function should handle out-of-bounds errors gracefully and reject the promise with an appropriate error message.getValueList function should be able to handle ranges that exceed the bounds of the input array by either truncating the range or handling it in a way that ensures program stability.value fields of the items in the input JSON structure, maintaining their original hierarchical order.