es5异步
1
let ajax = function(callback) {
console.log("开始执行ajax函数");
setTimeout(function(){
callback&&callback.call()
},1000);
};
ajax(function(){
console.log("执行ajax函数的回调函数");
})
1.1 改进
let ajax = function(callback) {
console.log("开始执行ajax函数");
setTimeout(function(){
callback&&callback.call()
},1000);
};
let f1=function(){
console.log("执行ajax函数的回调函数");
}
ajax(f1)
1.2 改进
let ajax = function(sql,done) {
console.log("开始执行ajax函数");
setTimeout(function(){
done(sql)
},1000);
};
let abc=function(sql){
console.log(sql);
}
let sql='select * from china'
ajax(sql,abc)
1.3 改进
let ajax = function(result,sql,done) {
console.log("开始执行ajax函数");
setTimeout(function(){
done(result,sql)
},1000);
};
let abc=function(result,sql){
result=100
console.log(sql);
}
let sql='select * from china'
let result
ajax(result,sql,abc)
2 es5异步转同步
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>异步变同步</title>
</head>
<body>
<script type="text/javascript"> var a = 0; window.onload = function(){
var a = function(){
setTimeout(function(){
a = 100; if(get){
get(a) } },5000) }() if(a){
document.write(a) }else if(!a){
var get = function(b){
a=b; document.write(a) } } } </script>
</body>
</html>
3
let ajax = function (sql, done) {
setTimeout(function () {
done()
}, 1000);
console.log("开始执行ajax函数");
};
let sql = 'dbsqlu'
ajax(sql, function () {
console.log(sql + "执行完毕");
})
es6异步
let sendok = false
let sendtoairport = () => {
return new Promise((resolve, reject) => {
if (sendok) {
setTimeout(() => {
resolve('send ok')
}, 1000);
}
else{
setTimeout(() => {
reject('send not ok')
}, 1000);
}
})
}
let p = sendtoairport()
p.then((value) => {
console.log(value) }, (reason) => {
console.log(reason) })
今天的文章es5异步分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/8918.html