通常而言,這3個關鍵字 都是用來「優雅」的處理ajax異步請求的
//es6的時候promise誕生,很好的解決了嵌套回調地獄,改良方案為鏈式回調。// es2017的時候誕生了async、await,這下異步直接沒有回調了,像同步一樣爽//沒有promise的時候$('button').click(()=>{let url='http://t.weather.sojson.com/api/weather/city/101010100';$.get(url,(res)=>{console.log(res)})})//有promise的時候(jq內部ajax增加了返回promise格式,之前不支持,只能嵌套回調)$('button').click(()=>{let url='http://t.weather.sojson.com/api/weather/city/101010100';$.get(url).then((res)=>{console.log(res)})})//有await時代(async只能用在返回promise代碼中,將其包裹,表明我這里邊異步。 用await表示 等一下我的異步 執行完再向下執行)$('button').click(async ()=>{let url='http://t.weather.sojson.com/api/weather/city/101010100';let res=await $.get(url)console.log(res)})
?
?
另外推薦優質博客:
https://blog.csdn.net/yanh_an
http://www.fromyan.com/
或百度搜索:YanH_an