What is wrong with this code for querying a native API?
What is wrong with this code for querying a native API?
To answer the interview question "What is wrong with this code for querying a native API?" let's analyze the provided code snippet and identify potential issues:
fetchToken() {
fetch('http://10.0.2.2:8163/extension/auth', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
})
.then((response) => console.log('successful fetchToken response: ', response.json()))
.catch((error) => console.log('fetchToken error: ', error))
.done();
}
Incorrect Handling of response.json():
response.json() method returns a promise, but it is being logged directly without waiting for the promise to resolve. This will not log the actual JSON response but rather the promise object..then() to handle the resolved JSON data.fetchToken() {
fetch('http://10.0.2.2:8163/extension/auth', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
})
.then((response) => response.json())
.then((data) => console.log('successful fetchToken response: ', data))
.catch((error) => console.log('fetchToken error: ', error))
.done();
}
Error Handling:
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.