function exists(url) {
return new Promise((resolve, reject) => {
if (url) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(true);
} else {
resolve(false);
}
}
};
xhr.open("Head", url, true);
xhr.send(null);
}else{
resolve(false);
}
});
}
// 调用:
async load() {
...
if( await exists(path) ){ ... }
}
js 同步请求API接口返回
最新推荐文章于 2023-11-30 10:31:01 发布