CoffeeScript Promise递归链无法正确嵌入子集数据的技术问题
Solution to Recursively Embed Subset Data in Nested Structure
Let's fix your recursive function step by step. The core issues with your previous attempts were not properly handling asynchronous recursion (since get_data returns a Promise) and not correctly merging the subset data back into the parent object.
Correct Implementation
Here's the revised CoffeeScript function that achieves the desired nested structure:
get_datas = (id) -> # Fetch the top-level data for the given id get_data(id) .then (dataList) -> # Wait for all recursive operations to complete with Promise.all Promise.all dataList.map (data) -> # Build the base object structure: { [data.id]: { type, pid, ... } } resultObj = _.zipObject([data.id], [_.omit(data, 'id')]) parentEntry = resultObj[data.id] # Check if we need to fetch and embed subset data if data.type == "value1" # Recursively get subset data using the element's pid get_datas(data.pid) .then (subdatas) -> # Attach subset data to the parent entry parentEntry.subset_data = subdatas return resultObj else # No subset needed, return the base object directly return resultObj
Why This Works
Let's break down how this fixes your earlier issues:
Handling Asynchronous Operations:
- We wrap the
mapcall withPromise.allto ensure we wait for all recursiveget_datascalls to resolve before returning the final array. Your first attempt didn't await these Promises, so subset data never got embedded into the parent objects. - Each iteration in
mapreturns a Promise (either the base object immediately, or a Promise that resolves to the merged object after recursion completes).
- We wrap the
Properly Merging Subset Data:
- We first create the base
resultObjstructure, then grab a reference to the parent entry (parentEntry = resultObj[data.id]). - When we fetch the subset data, we directly assign it to
parentEntry.subset_databefore returning the completeresultObj. This ensures the subset is nested in the correct place, unlike your second attempt which returned recursive arrays instead of merging them into the parent object.
- We first create the base
Example Output
When you run this function, it will produce exactly the nested structure you're targeting:
[ {"1":{"type":"value","pid":"123"}}, {"2":{"type":"value1","pid":"451", "subset_data":[ {"4":{"type":"value","pid":"1234"}}, {"5":{"type":"value1","pid":"321","subset_data":[ {...}, {...} ] }}, {"6":{"type":"value2","pid":"111"} ] }}, {"3":{"type":"value3","pid":"132"}} ]
Key Fixes from Your Original Code
- First Attempt: You didn't use
Promise.allto wait for recursive Promises, so the subset data was never resolved and merged into the parent object. Thetheninsidemapcreated unresolved Promises that weren't awaited. - Second Attempt: You returned the recursive
get_datasresult directly instead of merging it into the parent object'ssubset_dataproperty, leading to nested arrays instead of the desired object structure.
内容的提问来源于stack exchange,提问作者sharingiscaring
相关产品推荐
相关产品推荐

