fetch("data/app.json") .then(response => { return response.json(); }) .then(jsondata => { that.onLoadAppJsonSuccess(jsondata); }); // exhibits.json fetch("data/exhibits.json") .then(response => { return response.json(); }) .then(jsondata => { that.exhibitsData = jsondata; }); // highPicture.json fetch("data/highPicture.json") .then(response => { return response.json(); }) .then(jsondata => { that.highPicData = jsondata; });能改成顺序执行吗
时间: 2024-03-02 08:54:14 浏览: 33
可以,可以使用 Promise 的 `then` 方法来实现顺序执行。具体来说,我们可以将第二个 `fetch` 方法和第三个 `fetch` 方法放在第一个 `fetch` 方法的回调函数中,这样就可以保证它们在第一个请求成功后才会被执行。具体的代码如下:
```
fetch("data/app.json")
.then(response => {
return response.json();
})
.then(jsondata => {
that.onLoadAppJsonSuccess(jsondata);
return fetch("data/exhibits.json");
})
.then(response => {
return response.json();
})
.then(jsondata => {
that.exhibitsData = jsondata;
return fetch("data/highPicture.json");
})
.then(response => {
return response.json();
})
.then(jsondata => {
that.highPicData = jsondata;
});
```
在这个代码中,我们将第二个和第三个 `fetch` 方法放在第一个 `fetch` 方法的回调函数中。在第一个请求成功后,我们会立即执行第二个 `fetch` 方法,并在其回调函数中执行第三个 `fetch` 方法。这样就可以保证它们按照顺序依次执行了。