question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to validate user input with async function?

See original GitHub issue

I have to validate a user email with my backend using a fetch call. Can I do this? I’m trying with async/await but does not work.

My code:

function resolveClientEmailAddress(email) {
   return new Promise(resolve => {
     let data = new FormData();
    data.append( "client", email);
    fetch(config.server.remote + "api/client-by-mail/", {
               method: "POST",
               body: data
            }).then(function(res){ return res.json(); }).then(function(data){
               resolve(data)
            }).catch(function (error) {
               console.log("CATCH ----> ERROR ===> ", error)
             });
         });
      }

      async function clientEmailValidator(email) {
         let response = await resolveClientEmailAddress(email);

         console.log("clientEmailValidator ===> ", response); // 10

         if (response.hasOwnProperty('error_message'))
            return response.error_message;
         else
            return true
      }

      let chatbotQuestionList = [{
         id: '1',
         message: 'Welcome',
         trigger: '2',
      },{
         id: '2',
         message: "Insert your email address",
         trigger: '3',
      },{
         id: '3',
         user: true,
         validator: clientEmailValidator,
         trigger: 'end',
      },{
         id: 'end',
         message: 'Ok',
         end: true,
      }];

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
thomas-negraultcommented, Jan 10, 2018

Yes that’s pretty much what I want ! With theses two props and this render for my component:

return <Loading/>

The behaviour is:

  1. The user enter its input
  2. The bot display the “loading/writings” dots as the ajax call is made
  3. When the ajax call is finish, the bubble of my component disapear and another one with the conditional step appears.

It is just a little bit weird that the bubble disappears and then reappears with the dots… can we set the botDelay to 0 for a single step ?

Edit: found it: the delay option on the step, sorry !

The render is amazing ! Thank you !

1reaction
LucasBassetticommented, Jan 10, 2018

Please try to add the asMessage: true and replace: true props in the custom step. Not sure if will work as you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

async js validation form - javascript - Stack Overflow
The problem is that to var isInvalid in checkValidity method is returned promise.
Read more >
javascript - await user input with async/await syntax
I would love to not have to only use async/await to wait for web resources to come back but also to wait for...
Read more >
Async Change Validation Example - Redux Form
To provide asynchronous validation, provide redux-form with an asynchronous validation function ( asyncValidate ) that takes an object of form values, ...
Read more >
Synchronous and asynchronous input validation with ag-Grid
This function will be used to validate the value entered by the user. The syncValidator is responsible for input validation and calling the...
Read more >
Async Validators | JET Developer Cookbook - Oracle
The async validator's validate method returns a Promise that resolves if validation passes and value is updated or rejects with an Error if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found