异步:1.定时器,2.ajax 3.事件处理,nodejs读取文件
promise
优点:没有太多嵌套关系,是一个链式编程。
//.then取结果 let p1 = new Promise(function(resove,reject){
$.ajax({
url:"./php/ok.php", success:function(res){
resole(res) } }) }) p1 .then(function(data){
let p2 = new Promise(function(resove,reject){
$.ajax({
url:"./php/ok.php", success:function(res){
resole(res) } }) }) return p2; }) .then(function(second){
console.log('成功的结果',res) })
讯享网
封装
讯享网<body> <button id="btn"> 点击发送ajax </button> </body> function axios(){
let p1 = new Promise(function(resove,reject){
$.ajax({
url:"./php/ok.php", success:function(res){
resole(res) } }) }) return p1; } axios().then(function(res){
console.log('结果',res) }) //async wait 是可以省略掉then 简单快捷,代码清晰 //async awiat 是Generator函数的语法糖 //点击 发送ajax document.getElementById("btn").onclick = async()=>{
//axios().then(function(res){
// console.log('结果',res) let res = await axios()//等执行完之后才会执行下面的代码 console.log('结果',res) }) } //调用axios函数 得到p1 new的promise
async + await 其实就是 generate + yield 的语法糖

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/115880.html