First things first, I made sure to write a quick demo of the issue I'm talking about here
But essentially, using the isomorphic-fetch
library, I'm running into an issue where I can't really get the value, or you might say, resolution, of the fetch()
function.
import fetch from "isomorphic-fetch";
async function test() {
return await fetch(";, { mode: "no-cors" });
}
let t = test();
console.log(t);
The oute of which is
Now I've also considered the other way of using fetch()
like this
fetch(";, { mode: "no-cors" })
.then(response => response.text())
.then(data => console.log(data));
which actually delivers a string, but I prefer doing it the first way, if possible? It's also very possible I'm not using fetch correctly.
First things first, I made sure to write a quick demo of the issue I'm talking about here https://codesandbox.io/s/exciting-swirles-7cs3s
But essentially, using the isomorphic-fetch
library, I'm running into an issue where I can't really get the value, or you might say, resolution, of the fetch()
function.
import fetch from "isomorphic-fetch";
async function test() {
return await fetch("https://google.", { mode: "no-cors" });
}
let t = test();
console.log(t);
The oute of which is
Now I've also considered the other way of using fetch()
like this
fetch("https://google.", { mode: "no-cors" })
.then(response => response.text())
.then(data => console.log(data));
which actually delivers a string, but I prefer doing it the first way, if possible? It's also very possible I'm not using fetch correctly.
Share Improve this question asked Jul 21, 2020 at 4:37 notacornnotacorn 4,1295 gold badges38 silver badges70 bronze badges 4- 2 You cannot turn something asynchronous into synchronous without blocking. – Derek 朕會功夫 Commented Jul 21, 2020 at 4:42
- so how do i block? @Derek朕會功夫 – notacorn Commented Jul 21, 2020 at 4:45
-
1
mode: "no-cors"
is certainly not what you want stackoverflow./questions/41921805/… – Kaiido Commented Jul 21, 2020 at 4:49 -
it works without
"no-cors"
on my puter, i just had to add it for codesandbox @Kaiido – notacorn Commented Jul 21, 2020 at 4:50
3 Answers
Reset to default 5Try it like this:
import fetch from "isomorphic-fetch";
async function test() {
const response = await fetch("https://google.", { mode: "no-cors" });
return response.text();
}
async function main() {
let t = await test();
console.log(t);
}
main();
You need to await the promise, and that means you need an async function.
fetch will return a promise, not a string. In your second example you call .text()
on it. You will have to do something similar in asyc/await
Use t.then(res => console.log(res));
it will return response
object.
As you have async
function test
and you are not await
ing it like await test()
so it will return promise
.
As per your ment you should be using await test()
. But you can only use await
inside async
so I suggest to use wrapper function like below.
import fetch from "isomorphic-fetch";
async function test() {
return await fetch("https://google.", { mode: "no-cors" });
}
async function wrapper() {
let t = await test();
console.log(t.text());
}
wrapper();