[英]Javascript – return multiple values to chained “then” functions
我有一個帶有幾個then函數的 fetch 調用。 在其中一個函數中,我想將兩個返回的變量傳遞給下一個然后是function。 但它不起作用。
.then(text => {
let content = new DOMParser().parseFromString(text, "text/html");
let main = content.querySelector('main').innerHTML;
let title = content.querySelector('title').innerHTML;
function thehtml() {
return {
main, title
}
}
})
.then(thehtml => {
let theParsedHtml = thehtml();
document.querySelector('main').innerHTML = theParsedHtml.main;
document.title = theParsedHtml.title;
})
在最后一個then()中訪問 thehtml() function 會拋出 a is not a function 錯誤。 感謝您的任何提示!
您可以像下面這樣修改代碼,通過它您可以在另一個then
方法中訪問title
和main
。
.then(text => {
let content = new DOMParser().parseFromString(text, "text/html");
let main = content.querySelector('main').innerHTML;
let title = content.querySelector('title').innerHTML;
return {
main, title
}
})
.then(thehtml => {
document.querySelector('main').innerHTML = thehtml.main;
document.title = thehtml.title;
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.