两个函数相互调用,最终完成后发送消息

Two functions calling each other, send message when finally done

提问人:nb_nb_nb 提问时间:5/10/2023 更新时间:5/10/2023 访问量:51

问:

我有 2 个相互调用的函数。满足所有条件时如何发送消息?

我的代码:

const func1 = async (arg) => {
  let table_id = result.data.internal_id;
  const response = await axios_Func({ graphQL query });
  console.log(`${response.data.name} - ${table_id}`);
  return func2({ query: graphQL_query, table_id });
}

func2 = async (result, table_id) => {
  let dev = await getAllTables({ query: tables(123) });
  let prod = await getAllTables({ query: tables(321) });
  let prod_id = new Set();
  result.data.data.table.table_fields.forEach((d) => {
    if (d. connector != null && !(d.name in dev))
      prod_id.add(prod[d.name]["id"]);
  });
  prod_id = [...prod_id];
  prod_id.map((x) => createOne({ query: graphQL_query(x) });
});

const callback = () => console.log("all done");

现在我的函数返回:

xyz - M5WBmnWs 
abc - QrqJ91Rq
qwe - VP0B0KEt

我该如何打电话?并使我的输出如下所示:callback

xyz - M5WBmnWs 
abc - QrqJ91Rq
qwe - VP0B0KEt
all done
JavaScript 函数 ecmascript-6 async-await 回调

评论


答:

0赞 jabaa 5/10/2023 #1

等待函数调用,最后调用:callback

await func1();
callback();
1赞 Roberto Yoc 5/10/2023 #2

我不明白你想做什么,但这应该有效:

const func1 = async (arg) => {
  let table_id = result.data.internal_id;
  const response = await axios_Func({ graphQL query });
  console.log(`${response.data.name} - ${table_id}`);
  await func2({ query: graphQL_query, table_id });
}

func2 = async (result, table_id) => {
  let dev = await getAllTables({ query: tables(123) });
  let prod = await getAllTables({ query: tables(321) });
  let prod_id = new Set();
  result.data.data.table.table_fields.forEach((d) => {
    if (d. connector != null && !(d.name in dev))
      prod_id.add(prod[d.name]["id"]);
  });
  prod_id = [...prod_id];
  prod_id.map((x) => createOne({ query: graphQL_query(x) });
  // if createOne is a promise you need:
  // return Promise.all(prod_id.map((x) => createOne({ query: graphQL_query(x) }));
});

const callback = () => console.log("all done");

const execute = async () => {
  await func1()
  callback()
}