Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将api调用的结果(使用基于promise的Axios库)返回到调用范围?_Javascript - Fatal编程技术网

Javascript 如何将api调用的结果(使用基于promise的Axios库)返回到调用范围?

Javascript 如何将api调用的结果(使用基于promise的Axios库)返回到调用范围?,javascript,Javascript,我知道这个话题有很多问题。我读过很多,但无法将这些信息转化为我的情况,因为有许多框架既可以用于承诺,也可以用于异步HTTP请求。我读得越多,就越糊涂。因此,我恳请您对此给予帮助 任务我会尽量让它具体化 这就是我想做的: 用户输入一个搜索词,通过提交api调用Wikimedia api,请求Wikipedia页面提供该搜索词。这将生成一个页面列表,可以是普通页面或元页面。我想过滤掉元页面 // the first call to the API, requesting the sources(pa

我知道这个话题有很多问题。我读过很多,但无法将这些信息转化为我的情况,因为有许多框架既可以用于承诺,也可以用于异步HTTP请求。我读得越多,就越糊涂。因此,我恳请您对此给予帮助 任务我会尽量让它具体化

这就是我想做的:

用户输入一个搜索词,通过提交api调用Wikimedia api,请求Wikipedia页面提供该搜索词。这将生成一个页面列表,可以是普通页面或元页面。我想过滤掉元页面

// the first call to the API, requesting the sources(pages)
axios.get(url, {params})
  .then(function (res) {
    const html = res.data[1].map((source) => {
      // here I want to filter meta pages 
      if (isNotMetaPage(source)) {
        return `
          <div class='sources_list_entry'>
            <p> ${source} </p>
          </div>
        `
      }
    }).join('')
显然,此函数返回
未定义的
,因为请求是异步的,并且在API答复之前返回结果。将结果记录到
内的控制台时,则
-块中的值正确

但我如何暂停执行,直到返回结果?我读了很多书,说我不应该进行同步调用,但在这里如何避免呢


谢谢你的帮助

实际上,您可以链接
then()
函数,因此在您的情况下,您可以按照以下步骤操作:

axios.get(url, {params})
    .then(function (res) {
        const html = res.data[1].map((source) => {
        // here I want to filter meta pages 
        isNotMetaPage(source).then(function(isNotMeta) {
            if(isNotMeta){
                //do something asynchronously
            }
        });
}).join('')


function isNotMetaPage (title) {

    const encodedTitle = encodeURI(title)
    const query = `?action=query&titles=${encodedTitle}&prop=revisions&rvprop=content&format=json&origin=*`

    return axios.get(url.concat(query))
        .then(function (response) {
            const wikiPage = parseWikiMarkup(response);
            return (wikiPage.type === 'page');
    });
}

您不能“暂停”javascript执行。显然,您已经开始理解异步,并且您的数据将在稍后的某个时间点出现。此时(在
中,然后在
中),当您的数据可用时,调用测试函数,将数据传递给它。例如,
testTheResult(result)
。是的,我理解这个问题,只是不知道如何解决它。我需要调用的结果,因为没有它,它返回未定义的,并且if块从未执行。@Flip没有解决方案。您必须延迟使用结果的代码。
axios.get(url, {params})
    .then(function (res) {
        const html = res.data[1].map((source) => {
        // here I want to filter meta pages 
        isNotMetaPage(source).then(function(isNotMeta) {
            if(isNotMeta){
                //do something asynchronously
            }
        });
}).join('')


function isNotMetaPage (title) {

    const encodedTitle = encodeURI(title)
    const query = `?action=query&titles=${encodedTitle}&prop=revisions&rvprop=content&format=json&origin=*`

    return axios.get(url.concat(query))
        .then(function (response) {
            const wikiPage = parseWikiMarkup(response);
            return (wikiPage.type === 'page');
    });
}