async+fn
- ajax 异步刷新
//window.XMLHttpRequest ajax基础
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest(); // new 一个对象
// readyState 4: 请求已完成,且响应已就绪 3 2 1 0
// 响应状态 200: "OK" 404: 未找到页面
xhr.onreadystatechange(()=>{
if (xhr.readyState==4 && xhr.status==200){
console.log(xhr.responseText;);
}
});
xhr.open(method,url,async);// post url true 异步交互
xhr.send();
}
- script.src jsonp 跨域
- 同源是指,域名,协议,端口相同
- 基于安全考虑,当前域不能访问其他域的东西
- 动态创建<script>标签,然后利用<script>的src 不受同源策略约束来跨域获取数据
- var script = document.createElement("script");
script.src = "url+?q=javascript&count=1&callback=handleResponse";
document.body.insertBefore(script, document.body.firstChild);
// 异步处理函数
function handleResponse(){}
- async 异步
- async + function(){} // 通过Promise.resolved()将字符串封装成了一个Promise对象返回
- 返回的Promise对象,我们就是用then方法来处理
await+promise
- await 等待
- await关键字也不能单独使用,是需要使用在async方法中,用于等待异步返回的promise 执行结果
- await后面只能 是Promise对象么?答案是否定的,可以是字符串,布尔值,数值以及普通函数
{ async getData(){ // // 发起Http请求函数,返回Promise对象 const result = await htpReq(); return result; } // 方法一 loadList(){ getData.then((result)=>{ if(result){ console.log(result); } }) } // 方法二 async loadList(){ const result = await getData(); if(result){ console.log(result); } } }
https://blog.csdn.net/tcy83/article/details/80544048 es7 关键字 async 详细解释