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

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

是否可以將 Promise 用作函數(shù)參數(shù)的默認(rèn)值?

是否可以將 Promise 用作函數(shù)參數(shù)的默認(rèn)值?

猛跑小豬 2022-08-27 10:54:40
我想使用Promise的值作為函數(shù)的默認(rèn)參數(shù),如下所示:_drawElement(selector, html = this._htmlFrom(selector)) {  console.log(html);}_htmlFrom(templateName) {  var templatePath = `${this._templates}/${templateName}.html`;  return fetch(templatePath)    .then((response) => response.ok ? response.text() : null)}但我希望html是一個字符串,因?yàn)槲铱梢杂胔tmlFrom函數(shù)以外的其他方式傳遞它。在這里,我有一個承諾,我不知道如何檢索字符串值(或null)。好吧,所以按照Soverevisionerformance的建議,我這樣做了:async _drawElement(selector, html = this._htmlFrom(selector)) {var tags = document.querySelectorAll(selector);if (tags.length == 1) {  console.log(html);  var isPromise = typeof html.then == 'function';  if (isPromise) {    html = await html;  }...}但是idk為什么,_htmlFrom函數(shù)被調(diào)用,即使我用html的值調(diào)用函數(shù)。(為什么html是一個承諾,即使我給出一個字符串作為參數(shù))
查看完整描述

1 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊

您需要的HTML是異步檢索的,不幸的是,沒有辦法在參數(shù)列表中等待它完成。您必須在函數(shù)的第一行上等待它:


async _drawElement(selector, html) {

  if (html === undefined) {

    html = await this._htmlFrom(selector);

  }

  console.log(html);

}


查看完整回答
反對 回復(fù) 2022-08-27
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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