嗨,我想同時呼叫這些 Fetch 呼叫,所以我顯示了平板電腦、智能手機和筆記本電腦。我該怎么做呢?我用異步函式嘗試了一些東西,但沒有成功。
代碼:
onInit: function () {
const tabletUrl = '/api/tablets?limit=1000&offset=0';
fetch(tabletUrl).then(res => res.json()).then(res => {
const dataModel = new JSONModel();
dataModel.setData({
items: res
});
this.getView().setModel(dataModel, "aribadevices")
})
const notebookUrl = '/api/notebooks?limit=1000&offset=0';
fetch(notebookUrl).then(res => res.json()).then(res => {
const dataModel = new JSONModel();
dataModel.setData({
items: res
});
this.getView().setModel(dataModel, "aribadevices")
})
const smartphonesUrl = '/api/smartphones?limit=1000&offset=0';
fetch(smartphonesUrl).then(res => res.json()).then(res => {
const dataModel = new JSONModel();
dataModel.setData({
items: res
});
this.getView().setModel(dataModel, "aribadevices")
})
},
uj5u.com熱心網友回復:
您已經有異步同時運行的請求。
我假設您想使用來自所有三個請求的資料設定一次資料模型。在這種情況下,我認為使用Promise.all結合您所有的 fetch 承諾將是一個很好的解決方案。
確保將來自組合承諾的回應展平,以便使用<Array>.flat.
const tabletUrl = '/api/tablets?limit=1000&offset=0';
const notebookUrl = '/api/notebooks?limit=1000&offset=0';
const smartphonesUrl = '/api/smartphones?limit=1000&offset=0';
Promise.all([fetch(tabletUrl), fetch(notebookUrl), fetch(smartphonesUrl)]).then(res => Promise.all(res.map(r => r.json()))).then(data => {
const dataModel = new JSONModel();
dataModel.setData({
items: data.flat()
});
this.getView().setModel(dataModel, "aribadevices");
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/392135.html
標籤:javascript xml 接口 sapui5
上一篇:我應該使用哪些工具進行雙向通信?
下一篇:改造回傳空值期望一個屬性
