<bdo id='xlxwG'></bdo><ul id='xlxwG'></ul>

    <small id='xlxwG'></small><noframes id='xlxwG'>

        <legend id='xlxwG'><style id='xlxwG'><dir id='xlxwG'><q id='xlxwG'></q></dir></style></legend>
      1. <tfoot id='xlxwG'></tfoot>
        <i id='xlxwG'><tr id='xlxwG'><dt id='xlxwG'><q id='xlxwG'><span id='xlxwG'><b id='xlxwG'><form id='xlxwG'><ins id='xlxwG'></ins><ul id='xlxwG'></ul><sub id='xlxwG'></sub></form><legend id='xlxwG'></legend><bdo id='xlxwG'><pre id='xlxwG'><center id='xlxwG'></center></pre></bdo></b><th id='xlxwG'></th></span></q></dt></tr></i><div id='xlxwG'><tfoot id='xlxwG'></tfoot><dl id='xlxwG'><fieldset id='xlxwG'></fieldset></dl></div>
      2. 使用异步/等待返回值意外获取 API

        Fetch API Using Async/Await Return Value Unexpected(使用异步/等待返回值意外获取 API)

            <bdo id='fqQjh'></bdo><ul id='fqQjh'></ul>
              <tbody id='fqQjh'></tbody>
            • <tfoot id='fqQjh'></tfoot>
              1. <legend id='fqQjh'><style id='fqQjh'><dir id='fqQjh'><q id='fqQjh'></q></dir></style></legend>
                • <i id='fqQjh'><tr id='fqQjh'><dt id='fqQjh'><q id='fqQjh'><span id='fqQjh'><b id='fqQjh'><form id='fqQjh'><ins id='fqQjh'></ins><ul id='fqQjh'></ul><sub id='fqQjh'></sub></form><legend id='fqQjh'></legend><bdo id='fqQjh'><pre id='fqQjh'><center id='fqQjh'></center></pre></bdo></b><th id='fqQjh'></th></span></q></dt></tr></i><div id='fqQjh'><tfoot id='fqQjh'></tfoot><dl id='fqQjh'><fieldset id='fqQjh'></fieldset></dl></div>

                  <small id='fqQjh'></small><noframes id='fqQjh'>

                  本文介绍了使用异步/等待返回值意外获取 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  函数如下:

                  const getUserIP = async () => {
                    let response = await fetch('https://jsonip.com/');
                    let json = await response.json();
                  
                    console.log(json.ip)
                    return json.ip;
                  };
                  

                  在控制台中,IP 地址按预期记录.但是,当我将IP 地址"保存到变量时:

                  In the console, the IP address is logged as expected. However, when I save the 'IP address' to a variable:

                  const ip = getUserIP();
                  

                  然后在控制台输入ip,值显示为:

                  And then type ip in the console, the value is shown as:

                  Promise { <state>: "fulfilled", <value>: "/* my IP here*/" }
                  

                  我在 YouTube 上观看过使用相同逻辑/语法的视频,尽管 API 不同,但它确实有效.我已经搜索了 Google 和 SO,但找不到具有相同问题的线程.

                  I've watched videos on YouTube who have used the same logic/syntax albeit for a different API, but it works. I've searched Google and SO and couldn't find a thread with the same issue.

                  我错过了什么?

                  谢谢.

                  推荐答案

                  异步函数返回 Promises,你需要按如下方式获取该值:

                  Async functions return Promises, you need to get that value as follow:

                  getUserIP().then(ip => console.log(ip)).catch(err => console.log(err));
                  

                  或者,您可以将async声明添加到调用函数getUserIP的主函数中:

                  Or, you can add the async declaration to the main function who calls the function getUserIP:

                  async function main() {
                      const ip = await getUserIP();
                  }
                  

                  这篇关于使用异步/等待返回值意外获取 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                  <small id='F0FCT'></small><noframes id='F0FCT'>

                • <legend id='F0FCT'><style id='F0FCT'><dir id='F0FCT'><q id='F0FCT'></q></dir></style></legend>

                      <tbody id='F0FCT'></tbody>

                    • <bdo id='F0FCT'></bdo><ul id='F0FCT'></ul>
                      <tfoot id='F0FCT'></tfoot>
                          <i id='F0FCT'><tr id='F0FCT'><dt id='F0FCT'><q id='F0FCT'><span id='F0FCT'><b id='F0FCT'><form id='F0FCT'><ins id='F0FCT'></ins><ul id='F0FCT'></ul><sub id='F0FCT'></sub></form><legend id='F0FCT'></legend><bdo id='F0FCT'><pre id='F0FCT'><center id='F0FCT'></center></pre></bdo></b><th id='F0FCT'></th></span></q></dt></tr></i><div id='F0FCT'><tfoot id='F0FCT'></tfoot><dl id='F0FCT'><fieldset id='F0FCT'></fieldset></dl></div>