2 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
報(bào)告樓上,這個(gè)不歸axios管
這個(gè)歸 promise 管,要想代碼好看,條件允許的情況下,使用 async 和await
還有樓主,代碼隨便改的,不一定完全正確,但就是這么個(gè)意思
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, // 這是一個(gè)對(duì)象
arr = [];
const self = this,
// 需要請(qǐng)求的字段
needQuest = ["attachUrl", "content"];
source.map(function(val) {
let obj = {};
needQuest.map(function(field) {
let url = val[field];
if (url && typeof url === "string") {
// 問(wèn)題出在這兒
let val= await parseHtml(url);
}
});
arr.push(Object.assign(val, obj));
});
// 返回的數(shù)據(jù)沒有請(qǐng)求到的數(shù)據(jù)
return arr;
添加回答
舉報(bào)