打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ES6---axios执行原理

ES6---axios执行原理

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中

http://www.axios-js.com/zh-cn/docs/

 

 

 


 

1. 

    axios.get('1111.json')
        .then(response => {
            console.log(response.data);

        })
        .catch(error => {
            console.log(error);
        });

 

console:

 

2. 

    axios.get('1111.json')
        .then(response => {
            console.log(response.data);

        })
        .catch(error => {
            console.log(error);
        });

    console.log(123);

 

console:

3. axios.get相当于new了一个promise,具体如下图:

 

4.

    async function tt() {
        await axios.get('1111.json')
            .then(response => {
                console.log(response.data);
            })
            .catch(error => {
                console.log(error);
            });
        console.log(123);
    };
    tt();

 

console:

 

 

 

5. 用await 先取出了数据

    async function tt() {
        await axios.get('1111.json')//先取出了数据
            .then(response => {
                console.log(response.data);
            })
            .catch(error => {
                console.log(error);
            });
        document.getElementById("aa").innerHTML = '<div>123</div>';
    };
    tt();

 

console:

 

 

6. 用await先获取数据,不然就先执行后面的:document.getElementById("aa").innerHTML = '<div>' + yy + '</div>';

    var yy = '';
    async function tt() {
        await axios.get('1111.json')//先通过URL从服务器获取数据,再显示到页面
            .then(response => {
                yy = response.data.username;
                console.log(response.data);
            })
            .catch(error => {
                console.log(error);
            });
        document.getElementById("aa").innerHTML = '<div>' + yy + '</div>';
        //用了await就先获取数据了
    };
    tt();

 

7. 或者把DOM操作的放在axios里面也可以 (前提内嵌的不是很复杂,很复杂的放在外面用await比较好)

    var yy = '';
    async function tt() {
        await axios.get('1111.json')//先通过URL从服务器获取数据,再显示到页面
            .then(response => {
                yy = response.data.username;
                console.log(response.data);
                document.getElementById("aa").innerHTML = '<div>' + yy + '</div>';
            })
            .catch(error => {
                console.log(error);
            });

    };
    tt();

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
axios发送请求,一篇搞定
第三方系统访问微搭低代码的后端API
看了就会的 Node.js 常用三方工具包
ES6---axios和RESTful
vue中使用axios
$.ajax,axios,fetch三种ajax请求的区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服