我想使用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ù))
是否可以將 Promise 用作函數(shù)參數(shù)的默認(rèn)值?
猛跑小豬
2022-08-27 10:54:40