亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js延遲寫入

js延遲寫入

牛魔王的故事 2019-02-13 17:13:34
在做一個數據處理的問題,中間會有ajax請求,最后請求到的數據不能拼接到數組中,這樣改怎么處理?或者有更好的方式解決這個問題?下面是兩個方法:
查看完整描述

2 回答

?
慕的地10843

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;


查看完整回答
反對 回復 2019-02-21
  • 2 回答
  • 0 關注
  • 539 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號