3 回答

TA貢獻1808條經驗 獲得超4個贊
像這樣嘗試:
import fetch from "isomorphic-fetch";
async function test() {
const response = await fetch("https://google.com", { mode: "no-cors" });
return response.text();
}
async function main() {
let t = await test();
console.log(t);
}
main();
您需要等待承諾,這意味著您需要一個異步函數。

TA貢獻1871條經驗 獲得超8個贊
使用t.then(res => console.log(res));它將返回response對象。
因為你有async功能test而且你沒有await像await test()這樣它會返回promise。
根據您的評論,您應該使用await test(). 但是你只能await在內部使用,async所以我建議使用如下的包裝函數。
import fetch from "isomorphic-fetch";
async function test() {
return await fetch("https://google.com", { mode: "no-cors" });
}
async function wrapper() {
let t = await test();
console.log(t.text());
}
wrapper();
添加回答
舉報