You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. Handling Asynchronous Operations:

    • We wrap the map call with Promise.all to ensure we wait for all recursive get_datas calls 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 map returns a Promise (either the base object immediately, or a Promise that resolves to the merged object after recursion completes).
  2. Properly Merging Subset Data:

    • We first create the base resultObj structure, 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_data before returning the complete resultObj. 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.

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.all to wait for recursive Promises, so the subset data was never resolved and merged into the parent object. The then inside map created unresolved Promises that weren't awaited.
  • Second Attempt: You returned the recursive get_datas result directly instead of merging it into the parent object's subset_data property, leading to nested arrays instead of the desired object structure.

内容的提问来源于stack exchange,提问作者sharingiscaring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:27:32