I implemented a login function to verify credentials on my backend server, but I am facing an issue with waiting for the server response. Despite following the es7-async-await.js guide and trying various async/await and promise techniques, the function still does not work properly. I have searched multiple forums for solutions, but I cannot seem to figure out what I am doing wrong.
Here is my function:
async getCredentials(pUser, pCipher) {
var url = new URL(serviceURL);
var params = {
user: pUser,
p: pCipher
}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
// wait for response from fetch call
let response = await fetch(url, {
method: 'get',
headers: { }
});
// proceed once promise is resolved
let data = await response.json();
// proceed only when second promise is resolved
return data;
},
Here is how I call the function:
this.getCredentials(this.input.username, cipher)
.then(data => this.checkResponse = data.items)
.catch(reason => console.log(reason.message))
console.log("data: >>>>> ",this.checkResponse);
The outcome:
https://i.stack.imgur.com/tIyOj.png
The data always returns empty because the function does not wait for the response.