第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

承諾既解決又拒絕

承諾既解決又拒絕

慕俠2389804 2022-09-02 17:03:29
似乎我的應(yīng)許同時(shí)回歸真實(shí)和虛假??刂婆_(tái)返回“未定義”,然后返回“出現(xiàn)問(wèn)題”。數(shù)據(jù)在這些下面返回,表明它實(shí)際上并沒(méi)有等待承諾。下面是被調(diào)用的函數(shù):module.exports = (url) => {  return new Promise((resolve, reject) => {    axios({      method: 'get',      url: url    })      .then(response => {        const html = response.data        const $ = cheerio.load(html)        const songtable = $('.chart-list__elements > li')        const topsongs = []        songtable.each(function () {          const rank = $(this).find('.chart-element__rank__number').text()          if (rank == 11) return false;          const name = $(this).find('.chart-element__information__song').text()          const artist = $(this).find('.chart-element__information__artist').text()          topsongs.push({            rank,            name,            artist          })        })        resolve()        return topsongs;      })      .catch(reject("something went wrong"))    })}來(lái)自呼叫者:componentDidMount() {    const top_songs = topsongs('https://www.billboard.com/charts/hot-100')    .then(console.log(top_songs))    .catch(err => console.log(err))  }謝謝,我是Promise的新手,并且?guī)缀鯂L試了所有方法。盡管有異步 axios() 調(diào)用,但我有一個(gè) Promise 的原因是它沒(méi)有異步執(zhí)行并返回未定義的數(shù)據(jù)。
查看完整描述

2 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊

.catch(reject("something went wrong"))

您需要將函數(shù)傳遞給 。catch


您正在立即調(diào)用并傳遞其返回值。reject


您還在使用嵌套的承諾反模式。


axios返回一個(gè)承諾。無(wú)需創(chuàng)建另一個(gè)。


module.exports = (url) =>

  axios({

    method: "get",

    url: url,

  })

    .then((response) => {

      const html = response.data;

      const $ = cheerio.load(html);

      const songtable = $(".chart-list__elements > li");

      const topsongs = [];

      songtable.each(function () {

        const rank = $(this).find(".chart-element__rank__number").text();

        if (rank == 11) return false;

        const name = $(this).find(".chart-element__information__song").text();

        const artist = $(this)

          .find(".chart-element__information__artist")

          .text();

        topsongs.push({

          rank,

          name,

          artist,

        });

      });

      return topsongs;

    })

    .catch(() => {throw "something went wrong"});

(將拋出的錯(cuò)誤替換為通用的“出現(xiàn)問(wèn)題”似乎沒(méi)有幫助。如果沒(méi)有那個(gè)接聽(tīng)電話,你可能會(huì)過(guò)得更好)


查看完整回答
反對(duì) 回復(fù) 2022-09-02
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

你已經(jīng)有了一個(gè)承諾,只是回報(bào)它。


  return axios({

      method: 'get',

      url: url

    })

      .then(response => {

        const html = response.data

        const $ = cheerio.load(html)

        const songtable = $('.chart-list__elements > li')

        const topsongs = []

        songtable.each(function () {

          const rank = $(this).find('.chart-element__rank__number').text()

          if (rank == 11) return false;

          const name = $(this).find('.chart-element__information__song').text()

          const artist = $(this).find('.chart-element__information__artist').text()


          topsongs.push({

            rank,

            name,

            artist

          })

        })

        return topsongs;

      })

而僅僅對(duì)于“句法糖”,使所有內(nèi)容都更容易閱讀:async/await


module.exports = async (url) => {

   const { data } = await axios({method:'get',url});

   const $ = cheerio.load(data);


   ...


   return topsongs;

}


查看完整回答
反對(duì) 回復(fù) 2022-09-02
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)