【发布时间】:2020-09-11 09:29:43
【问题描述】:
'use strict';
const url = `https://www.exapmle.com/mediainfo?`
function getMediaInfo(videoid) {
fetch(url + videoid)
.then(response => {
if (response.ok === true) {
return response.text()
} else {
throw new error("HTTP status code" + response.status);
}
})
.then(text => {
const parser = new DOMParser();
const doc = parser.parseFromString(text, "text/xml");
console.log(doc) //I want to make 'doc' as getMediaInfo()'s return value
})
.catch(error => {
console.error(error)
});
}
我想将值 'doc' 作为 getMediaInfo() 的返回值。
但我不知道该怎么做。有什么帮助吗?
【问题讨论】:
-
您需要返回 fetch 值并在 then 块中返回 doc 变量。
标签: javascript promise fetch