So I have this JS code:
function createServerSideDatasource() {
return {
getRows: function(params) {
console.log('[Datasource] - rows requested by grid: ', params.request);
let response = getDataFromBackendForDataSource(params.request.startRow, params.request.endRow);
console.log(response);
},
};
}
function getDataFromBackendForDataSource(startRow, endRow) {
axios.get(
`./?startRow=${startRow}&endrow=${endRow}`, { 'contentType': 'application/json' }
).then((response) => {
let toSend = response.data
return toSend
}).catch((error) => {
alert(error);
});
}
The console.log() returns undefined and I don't really understand why.
After some research, I've understood that promises are just syntactic sugar for async/await.
With this information, I expected my code to work as follows:
when the call to getDataFromBackendForDataSource happens
the GET request has launched
createServerSideDatasource waits until the request has finished. (so no console.log yet)
the request has finished. toSend is returned
toSend is assigned to response in createServerSideDatasource
response is logged to the console (not undefined)
but it looks like my thinking is wrong, because I get undefined.
Am I missing something? Why is response undefined?
Thanks.