fetch(file)
.then(x => x.text())
.then(y => myDisplay(y));
Try it Yourself »
Fetch is based on async and await. The example might be easier to understand like this:
async function getText(file) {
let x = await fetch(file);
let y = await x.text();
myDisplay(y);
}
Try it Yourself »
Use understandable names instead of x and y:
async function getText(file) {
let myObject = await fetch(file);
let myText = await myObject.text();
myDisplay(myText);
}
Try it Yourself »
The fetch()
method starts the process of fetching a resource from a server.
The fetch()
method returns a Promise that resolves to a Response object.
? No need for XMLHttpRequest anymore.
fetch(file)
Parameter | Description |
file | Optional. The name of a resource to fetch. |
Type | Description |
Promise | A Promise that resolves to a Response object. |
fetch()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
fetch()
is not supported in Internet Explorer 11 (or earlier).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!