Back

Technologies:

javascriptjavascript
avatar
Tolerim
a day ago

Reworded: After completing the code within the if statement, proceed to execute the else statement.

The given code includes a function call to createOne(123) which in turn calls createFunc, and you want to make sure the if statement inside createFunc runs completely before the else block is reached. This can be achieved by waiting for the promise returned by the if block to resolve before executing the else block. Here's a modified version of the code that implements this:
const mainFunc = (fn) => {
  return async (query_name, ...args) => {
    const result = await axiosFunc(queryname);
    try {
      return fn(result, ...args);
    } catch (e) {
      console.log(e);
    }
  };
};

const createOne = mainFunc((result) => {
  let tableid = result.data.internalid;
  console.log('Created');
  axios_Func({ graphQL query }).then((response) => {
     createFunc({ query: graphQLquery, tableid}).then((res) => res);
  });
});

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(async (d) => {
    if (d.connector != null && !(d.name in dev)) {
      prodid.push(prod[d.name]["internalid"]);
    }
  });
  prodid = [...new Set(prodid)];
  console.log(prod_id);
  if (prod_id.length !== 0) {
      for (let x of prod_id) {
          await createOne({ query: graphQL(x) });
      }
  } else {
      await otherFunc(result, table_id);
  }
});

const otherFunc = pipefyFunction(async(result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  result.data.forEach((d) => {
    if (d.name in dev) {
      axios_DevFunc({ graphQL query }).then((response) => {
        console.log(`created from dev`);
        console.log(JSON.stringify(response.data, null, 4));
      });
    } else if (d.connector == null) {
      axios_Func({ graphQL query }).then((response) => {
        if (response.data.errors) {
          // rerun function on errors
        } else console.log(JSON.stringify(response.data, null, 4));
      });
    } 
  });
});
To address the circular structure error you received, you can use the JSON.stringify() method to convert the data returned from the API call to a string. Here's a modified version of the code that implements this:
const mainFunc = (fn) => {
  return async (query_name, ...args) => {
    const result = await axiosFunc(queryname);
    try {
      return fn(result, ...args);
    } catch (e) {
      console.log(e);
    }
  };
};

const createOne = mainFunc((result) => {
  let tableid = result.data.internalid;
  console.log('Created');
  axios_Func({ graphQL query }).then((response) => {
     createFunc({ query: graphQLquery, tableid}).then((res) => res);
  });
});

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(async (d) => {
    if (d.connector != null && !(d.name in dev)) {
      prodid.push(prod[d.name]["internalid"]);
    }
  });
  prodid = [...new Set(prodid)];
  console.log(prod_id);
  if (prod_id.length !== 0) {
      for (let x of prod_id) {
          await createOne({ query: graphQL(x) });
      }
  } else {
      await otherFunc(result, table_id);
  }
});

const otherFunc = pipefyFunction(async(result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  result.data.forEach((d) => {
    if (d.name in dev) {
      axios_DevFunc({ graphQL query }).then((response) => {
        console.log(`created from dev`);
        console.log(JSON.stringify(response.data, null, 4));
      });
    } else if (d.connector == null) {
      axios_Func({ graphQL query }).then((response) => {
        if (response.data.errors) {
          // rerun function on errors
        } else console.log(JSON.stringify(response.data, null, 4));
      });
    } 
  });
});
Now, the code will wait for the if statement to finish running before executing the else block, and will use JSON.stringify() to convert the API response to a string before logging it.
Answers(1)
avatar
Tolerim
a day ago
Verified Answer
The error message "Converting circular structure to JSON" occurs when trying to convert an object with circular references to JSON. In your code, it seems like you are trying to stringify an object that has a circular reference, which is causing this error message. To fix this issue, you can use the JSON.stringify() method with a replacer function parameter that handles circular references. Here is an example of how you can use this approach:
const otherFunc = pipefyFunction(async (result, table_id) => {
  let dev = await getTabs({ query: tables(123) });
  result.data.forEach((d) => {
    if (d.name in dev) {
      axios_DevFunc({ graphQL query }).then((response) => {
        console.log(`created from dev`);
        console.log(JSON.stringify(response.data, getCircularReplacer(), 4));
      });
    } else if (d.connector == null) {
      axios_Func({ graphQL query }).then((response) => {
        if (response.data.errors) {
          // rerun function on errors
        } else 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; }
      seen.add(value);
    }
    return value;
  };
}
In this example, we have defined a getCircularReplacer() function that returns a replacer function that can handle circular references. The getCircularReplacer() function uses a WeakSet to keep track of objects that have been seen so far, and skips over any circular references that have already been visited. To ensure that createFunc finishes running before calling otherFunc, you can return a Promise from createFunc that resolves when all asynchronous operations have completed. Here is an example of how you can modify createFunc to return a Promise:
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);
  
  let promises = [];
  if (prod_id.length !== 0) {
    prod_id.forEach((x) =>promises.push(createOne({ query: graphQL(x) })));
    await Promise.all(promises);
  } else {
    await otherFunc(result, table_id);
  }
});
In this example, we have created an empty array promises to store the promises returned by createOne. We have modified the if statement to push each createOne promise into the promises array, and to use Promise.all(promises) to wait until all promises have resolved before continuing. We have also added await before otherFunc(result, table_id) to ensure it does not get called until after the if statement is fully executed.
;