我為我的項目制作日歷,需要使用“CalendarPicker()函數”中服務器上的服務數據。我從服務器獲取數據作為承諾,并嘗試在“function CalendarPicker()”中使用它,但總是未定義。我怎么能強迫它?
async function _getMeetingsData() {
let response = await fetch('calendar');
let data = await response.json();
data = JSON.stringify(data);
return data;
}
function CalendarPicker() {
//Get meeting data
this.meetingsData = _getMeetingsData();
this._insertNavigationButtons();
this._insertHeaderIntoCalendarWrapper();
this._insertCalendarGridDaysHeader();
this._insertDaysIntoGrid();
this._insertCalendarIntoWrapper();
}
const myCalender = new CalendarPicker();
您的數據需要有一個
async
構造函數(可以說是奇數)或await
,因為每個async
函數都隱式地是一個承諾,即使它同步地返回一個值。。。所以:這是可行的,但如果您有任何需要該數據的原型方法,則最好存儲以下承諾: