I think the problem is becoming an asynchronous / synchronous method?
Yes. You (mostly) used the original promise correctly fetch(), but text()also returns the promise. So:
users.then(response => response.text())
.then(json => {
console.log(json);
})
.catch(error => {
});
fetch("https://jsonplaceholder.typicode.com/users")
.then(response => response.text())
.then(json => {
log("typeof json: " + typeof json);
log(json);
})
.catch(error => {
});
function log(msg) {
var p = document.createElement("pre");
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
Run codeHide resultfetch(), text().
text(), (, JSON).
fetch() text(), - .
, . , . , (, Node) .
JSON, json(), text(), :
users.then(response => response.json())
.then(arrayOfUsers => {
console.log(arrayOfUsers);
})
.catch(error => {
});
fetch("https://jsonplaceholder.typicode.com/users")
.then(response => response.json())
.then(arrayOfUsers => {
log("typeof arrayOfUsers: " + typeof arrayOfUsers);
log("arrayOfUsers.length: " + arrayOfUsers.length);
})
.catch(error => {
});
function log(msg) {
var p = document.createElement("pre");
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
Hide result