- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Fetch API
JavaScript Asynchronous
JavaScript Fetch API
fetchrequests data from a server and returns a promise.
The examples below define a small stand-in for fetch so they run without a server.
It behaves like the real thing: it returns a promise for a response object.
Example
Example
javascript
// A stand-in for the network, so this example runs on its own.
function fetch(url) {
return Promise.resolve({
ok: true,
status: 200,
json: function () {
return Promise.resolve({ name: "Ada" });
}
});
}
fetch("/api/user")
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data.name);
});