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

确保createFunc中if逻辑执行完毕后调用otherFunc及解决JSON循环引用问题

问题修复:异步执行顺序与JSON循环引用问题

问题1:异步操作未等待导致分支逻辑提前执行

原代码存在多处异步操作未正确等待的问题,导致createFunc的if分支未执行完毕就触发else分支的otherFunc,以下是具体修复点:

  1. createOne函数未返回异步操作的Promise
    原createOne内部的异步调用未返回Promise,外层mainFunc无法等待其完成,修改后:
const createOne = mainFunc(async (result) => {
  let table_id = result.data.internal_id;
  console.log('Created');
  // 等待axios_Func完成后再执行createFunc
  const response = await axios_Func({ /* 补全你的graphQL query */ });
  return createFunc({ query: /* 补全graphQL_query */, table_id });
});
  1. createFunc中forEach遍历异步操作未等待
    forEach不会等待异步函数执行,替换为Promise.all确保所有createOne调用完成后再继续:
if (prod_id.length !== 0) {
  // 等待所有createOne异步操作结束
  await Promise.all(prod_id.map(x => createOne({ query: graphQL(x) })));
} else {
  await otherFunc(result, table_id);
}
  1. otherFunc内部异步操作统一用await处理
    将then改为await,同时用Promise.all等待所有遍历中的异步操作完成:
const otherFunc = pipefyFunction(async (result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  await Promise.all(result.data.map(async (d) => {
    if (d.name in dev) {
      const response = await axios_DevFunc({ /* 补全graphQL query */ });
      console.log(`created from dev`);
      console.log(JSON.stringify(response.data, getCircularReplacer(), 4));
    } else if (d.connector == null) {
      const response = await axios_Func({ /* 补全graphQL query */ });
      if (response.data.errors) {
        // 这里添加错误重试逻辑,比如重新调用当前函数
        // await otherFunc(result, table_id);
      } else {
        console.log(JSON.stringify(response.data, getCircularReplacer(), 4));
      }
    } 
  }));
});

问题2:JSON循环引用错误修复

当response.data存在循环引用对象时,JSON.stringify会报错,可自定义replacer函数处理:

// 处理循环引用的工具函数
function getCircularReplacer() {
  const seen = new WeakSet();
  return (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) {
        return '[Circular Reference]'; // 替换循环引用为提示文本,也可返回undefined忽略该属性
      }
      seen.add(value);
    }
    return value;
  };
}

使用方式:

console.log(JSON.stringify(response.data, getCircularReplacer(), 4));

完整修复后的代码

// 处理循环引用的工具函数
function getCircularReplacer() {
  const seen = new WeakSet();
  return (key, value) => {
    if (typeof value === "object" && value !== null) {
      if (seen.has(value)) {
        return '[Circular Reference]';
      }
      seen.add(value);
    }
    return value;
  };
}

const mainFunc = (fn) => {
  return async (query_name, ...args) => {
    const result = await axios_Func(query_name);
    try {
      return fn(result, ...args);
    } catch (e) {
      console.log(e);
    }
  };
};

const createOne = mainFunc(async (result) => {
  let table_id = result.data.internal_id;
  console.log('Created');
  const response = await axios_Func({ /* 补全你的graphQL query */ });
  return createFunc({ query: /* 补全graphQL_query */, table_id });
});

const createFunc = mainFunc(async(result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  let prod = await getTabs({ query: tables(321) });
  let prod_id = [];
  result.data.forEach((d) => {
    if (d.connector != null && !(d.name in dev)) {
      prod_id.push(prod[d.name]["internal_id"]);
    }
  });
  prod_id = [...new Set(prod_id)];
  console.log(prod_id);
  
  if (prod_id.length !== 0) {
    await Promise.all(prod_id.map(x => createOne({ query: graphQL(x) })));
  } else {
    await otherFunc(result, table_id);
  }
});

const otherFunc = pipefyFunction(async (result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  await Promise.all(result.data.map(async (d) => {
    if (d.name in dev) {
      const response = await axios_DevFunc({ /* 补全graphQL query */ });
      console.log(`created from dev`);
      console.log(JSON.stringify(response.data, getCircularReplacer(), 4));
    } else if (d.connector == null) {
      const response = await axios_Func({ /* 补全graphQL query */ });
      if (response.data.errors) {
        // 这里添加错误重试逻辑
        // await otherFunc(result, table_id);
      } else {
        console.log(JSON.stringify(response.data, getCircularReplacer(), 4));
      }
    } 
  }));
});

关键修复总结

  • 所有异步操作必须用await或Promise.all等待完成,避免异步代码“逃逸”导致顺序混乱
  • 确保mainFunc包裹的函数返回Promise,让外层await能正确等待
  • 用自定义replacer函数处理JSON循环引用,避免JSON.stringify报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:17:05