Sunday 26 January 2020

Socket.io with async and await

I am new to web, and I am trying to learn how it all works by doing a small project for myself.I am trying to check if the string inputted by the user exists in the back end or not. And depending on the result I will show a message.//******// //Client// //******// const button = document.getElementById('button'); const textbox = document.getElementById('textbox'); button.addEventListener('click', function(){ if (checkEntry()){ alert("exists") } else{ alert("doesn't exist") } }); async function checkEntry(){ socket.emit('checkEntry', { entry: textbox.value }); var res; await socket.on('entryChecked', function(data){ res = data.result; }); return res; } //******// //Server// //******// io.on('connection', function(socket){ socket.on('checkEntry', function(data){ socket.emit('entryChecked', { result: check(data) }); }); }); function check(data){ //runs through an array and returns a Boolean } I am probably doing something wrong. Can you please correct my mistake and explain the reason behind it?

Submitted January 26, 2020 at 05:29PM by Derura

No comments:

Post a Comment