2 回答

TA貢獻1785條經驗 獲得超8個贊
報告樓上,這個不歸axios管
這個歸 promise 管,要想代碼好看,條件允許的情況下,使用 async 和await
還有樓主,代碼隨便改的,不一定完全正確,但就是這么個意思
function parseHtml(url, fun) {
return new Promise((resolve,reject)=>{
$.get(url, function(response, state, xhr) {
if (state === "success" && xhr.status === 200) {
let html = response.substring(response.indexOf("<body"), response.indexOf("</body>") + 7);
html = html.trim().replace(/^\<body*?>/, "").replace(/\<\/body\>$/, "");
resolve(html);
} else {
reject('some error')
}
});
}
}
async function eCollapseSourceComputed() {
let source = this.eCollapseSource, // 這是一個對象
arr = [];
const self = this,
// 需要請求的字段
needQuest = ["attachUrl", "content"];
source.map(function(val) {
let obj = {};
needQuest.map(function(field) {
let url = val[field];
if (url && typeof url === "string") {
// 問題出在這兒
let val= await parseHtml(url);
}
});
arr.push(Object.assign(val, obj));
});
// 返回的數據沒有請求到的數據
return arr;
添加回答
舉報