在promise中想要接收数据那就的在jsonp的回调函数将其推向已决状态也就是resolve,也可是说jsonp的回调函数就是为啦将promise推向已决阶段
案例演示
function jsonp(url, cd) {
return new Promise(resolve => {
let script = document.createElement('script');
if (url.includes("?")) {
url += "&callback=" + cd
} else {
url += "?callback=" + cd;
}
script.src = url;
window[cd] = resp => {
delete window[cd]
document.head.removeChild(script)
resolve(resp)
}
document.head.appendChild(script)
})
}
jsonp("http://developer.duyiedu.com/edu/testJsonp","aaa").then(resp =>{
console.log(resp)
})