简体   繁体   English

如何从另一个 function 中的异步获取中获取结果数据

[英]how to get result data from async fetch inside another function

I'm trying to use async fetch call to get data from the server but the result looks like an empty async function. what am I doing wrong?我正在尝试使用异步获取调用从服务器获取数据,但结果看起来像一个空的异步 function。我做错了什么?

this is my code这是我的代码

  function initAutoComplete (selector, params = []) {
    let config = {
      selector: selector, 
      data: {
        src: async () => {
          try {
            let myurl = `/component/get_states?country_id=64`; // http://localhost
            const response = await fetch(myurl)
            const source = await response.json();
            console.log('source', source); // ==> this doesn't even appear in the console log
            return source;
          } catch (err) {
            return err;
          }
        },
        keys: ["state_name"],
        cache: true,
      },
    };
    console.log('config', config.data);
    return config;
  }
  const asd = initAutoComplete('#state_name');
  console.log('asd', asd.data);

this is the result in the developer tools这是开发人员工具中的结果来自开发者工具的结果

although when I used postman to test the url, it shows the data as expected尽管当我使用 postman 测试 url 时,它显示的数据符合预期

[
    {
        "id": "17",
        "state_name": "Bali",
        "country_name": "Indonesia"
    },
    {
        "id": "16",
        "state_name": "Banten",
        "country_name": "Indonesia"
    },
    {
        "id": "7",
        "state_name": "Bengkulu",
        "country_name": "Indonesia"
    },
]

Your code shows calling initAutoComplete() , but all that does is define a config object that contains a data.src() function, but nothing you show ever calls that function.您的代码显示调用initAutoComplete() ,但所做的只是定义一个包含data.src() function 的config object,但您显示的任何内容都不会调用该 function。

So, per this code that function is never executed and never should be.因此,根据此代码,function 永远不会执行,也不应该执行。 The function has been defined, but not called. function 已定义,但未调用。

You would use this:你会用这个:

const asd = initAutoComplete('#state_name');
asd.data.src().then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});

to actually call that async function.实际调用async function。


In addition, return err is probably the wrong way to do error handling in your src() function. You WANT the promise that the async function returns to reject when you get an error.此外, return err可能是在您的src() function 中进行错误处理的错误方法。您想要async function 返回的 promise 以在您收到错误时拒绝。 As you're doing it now, it's quite a pain for the caller to tell the difference between a result and an error condition since you're just returning a value both ways.正如您现在所做的那样,调用者很难分辨结果和错误条件之间的区别,因为您只是以两种方式返回一个值。 The caller would probably have to check the resolved value to see if it was an instanceof Error to discern if the result was an error or not.调用者可能必须检查已解析的值以查看它是否是一个 instanceof Error 以辨别结果是否为错误。

If you remove the try/catch in your src() function, then the reject will happen automatically which is likely what you want.如果您删除src() function 中的try/catch ,那么拒绝将自动发生,这很可能是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 React Native 中的函数中的 fetch 获取数据 - How to get data from fetch inside a function in React Native 如何从Firestore中获取数据以及如何在其中存储其他数据(通过引用) - How to fetch data from firestore and inside it fetch another data (by reference) 在另一个异步函数中从诺言解析中获取结果 - Get result from promise resolve in another async function 如何在异步函数中设置变量=函数结果中的值 - How to set variable = a value from a function result inside async function 如何从 WatermelonDB 中的 fetch() 查询结果中获取 ._raw 中的实际数据? - How to get the actual data inside ._raw from fetch() query result in WatermelonDB? 如何将一个异步 function 的结果用于另一个 - How to use result from one async function to another 如何从在 Node 中使用 fetch 的异步函数缓存数据 - How to cache data from async function that uses fetch in Node 从异步函数获取数据到另一个函数 React JS - get data from async function to another function React JS React-如何从获取POST获取异步数据 - React - how to get async data from fetch POST 如何从异步 function 获取数据并将其用作另一个异步 function 上的 json 的属性值? - How to get the data from a async function and used that as a property value for a json on another asyn function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM