There is a "reviver" function that comes with JSON.parse (eg: "JSON.parse using reviver function" ).
How can I use this "reviver" with response.json ? for instance:
fetch(url)
.then(a => a.json({reviver})) // unfortunately not working
.then(...)
I have a workaround:
fetch(url)
.then(a => a.text())
.then(b => new Promise((resolve) => resolve(JSON.parse(b, reviver))))
.then()
but it uses one more step, which seems useless. Any better idea?
Your workaround is basically the best option but as mentioned, the extra promise is unnecessary.
fetch(url)
.then(response => response.text())
.then(text => JSON.parse(text, reviver))
// ...
You could do:
fetch(url).then((response) => {return response.json()}).then((json) => {console.log(json)});
Hope it helps;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.