JavaScript 递归调用 Axios.get [英] JavaScript Recursively call Axios.get

查看:22
本文介绍了JavaScript 递归调用 Axios.get的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 API,它返回具有多条记录的父子关系

第一次调用 API http://someAPI/getResult/?parent=null 会给我:

<代码>{父ID:空,编号:1,名称:'顶级元素'}

第二次调用应该是 http://someAPI/getResult/?parent=1 并且会返回:

<预><代码>[{父 ID:1,编号:2,名称:'二级元素第一元素'},{父 ID:1,编号:3,名称:'二级元素二级元素'}]

接下来应该是 http://someAPI/getResult/?parent=2 和 http://someAPI/getResult/?parent=3.那些人会送回自己的孩子,直到最后没有孩子回来.

如何编写一个递归函数,从顶层(ParentID = null)检索所有条目,直到最后一个孩子(没有进一步的记录)?

解决方案

使用手动堆栈处理递归可能是最简单的.

由于这是一个 async 函数,它会返回一个 Promise ,该 Promise 最终应解析为包含每个 parent=...

响应的对象

异步函数 getTree() {const idsToCheck = [空"];常量结果 = {};而(idsToCheck.length){const id = idsToCheck.shift();如果(结果[id]){//我们已经处理了这个节点继续;}const resp = await fetch("http://someAPI/getResult/?parent=" + id);if (!resp.ok) throw new Error("response not ok");const data = await resp.json();结果[id] = 数据;data.forEach((child) => {idsToCheck.push(child.Id);});}返回结果;}

(另一种使用实际递归函数调用的公式:

异步函数 getTree() {常量结果 = {};异步函数 populateResults(id) {如果(结果[id]){//我们已经处理了这个节点返回;}const resp = await fetch("http://someAPI/getResult/?parent=" + id);if (!resp.ok) throw new Error("response not ok");const data = await resp.json();结果[id] = 数据;for(让数据的孩子){等待 populateResults(results, child.Id);}}等待填充结果(空");返回结果;}

)

I have an API that returns a Parent Child relationship with multiple records

First call to API http://someAPI/getResult/?parent=null will give me:

{
   ParentID: null,
   Id: 1,
   Name: 'Top Level Element'
}

Second call should be to http://someAPI/getResult/?parent=1 and that will return:

[
  {
    ParentID: 1,
    ID: 2,
    Name:  'Second Level Element First Element'
  },
  {
    ParentID: 1,
    ID: 3,
    Name:  'Second Level Element Second Element'
  }
]

The next should be http://someAPI/getResult/?parent=2 and then http://someAPI/getResult/?parent=3. Those will return their own children until finally no children are returned.

How can I write a recursive function that will retrieve all entries from the top level (ParentID = null) until the last child (no further records)?

解决方案

It might be simplest to handle the recursion with a manual stack.

As this is an async function, it will return a Promise that should eventually resolve to an object containing the responses for each parent=...

async function getTree() {
  const idsToCheck = ["null"];
  const results = {};
  while (idsToCheck.length) {
    const id = idsToCheck.shift();
    if (results[id]) {
      // We've already processed this node
      continue;
    }
    const resp = await fetch("http://someAPI/getResult/?parent=" + id);
    if (!resp.ok) throw new Error("response not ok");
    const data = await resp.json();
    results[id] = data;
    data.forEach((child) => {
      idsToCheck.push(child.Id);
    });
  }
  return results;
}

(Another formulation that uses actual recursive function calls:

async function getTree() {
  const results = {};
  async function populateResults(id) {
    if (results[id]) {
      // We've already processed this node
      return;
    }
    const resp = await fetch("http://someAPI/getResult/?parent=" + id);
    if (!resp.ok) throw new Error("response not ok");
    const data = await resp.json();
    results[id] = data;
    for (let child of data) {
      await populateResults(results, child.Id);
    }
  }
  await populateResults("null");
  return results;
}

)

这篇关于JavaScript 递归调用 Axios.get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆