How to catch exception from asynchronous function?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to catch exception from asynchronous function?

voger
I have this example http://tinyurl.com/nay2rnr
I am trying to validate asynchronously the username. So on changeValue
of that field I call a function that asynchronously validates the
username and if it is invalid it throws an error. Unfortunately since
the call is asynchronous, when the function returns the caller has long
ended so there is no one to catch the exception. How can I handle the
exception?

I know I can handle the valid state of the item right there in the async
function but I want to know what is the proper way to handle
asynchronously the exception.

Also how can I use that function in a DRY way? The closer I came up is
this http://tinyurl.com/o2r56zv but when I press send, even if the field
is invalid I get "You can send" first and later I get "Nope..."

------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to catch exception from asynchronous function?

John Spackman-3
For your first example you have implemented validateUsername as an asynchronous function but asynchronous validations need to be implemented via qx.ui.form.AsyncValidator.  To handle the asynchronous exception, you need to catch it in an AsyncValidator’s callback.   You’re already doing this for the server based test, so perhaps you should merge your two async tests into one, or make validateUsername synchronous (EG because it only runs on the client).

In your second example, you’re getting “you can send” then “nope” because your Async validator calls setValid(true) and then later on setValid(false), so you get two “complete" events.  Also, as with your first example, your validateUsername() needs to either not throw any exceptions (because it is asynchronous) or needs to be called synchronously.  

Try this: http://tinyurl.com/ou2cml2



On 23/08/2015 23:08, "voger" <[hidden email]> wrote:

>I have this example http://tinyurl.com/nay2rnr
>I am trying to validate asynchronously the username. So on changeValue
>of that field I call a function that asynchronously validates the
>username and if it is invalid it throws an error. Unfortunately since
>the call is asynchronous, when the function returns the caller has long
>ended so there is no one to catch the exception. How can I handle the
>exception?
>
>I know I can handle the valid state of the item right there in the async
>function but I want to know what is the proper way to handle
>asynchronously the exception.
>
>Also how can I use that function in a DRY way? The closer I came up is
>this http://tinyurl.com/o2r56zv but when I press send, even if the field
>is invalid I get "You can send" first and later I get "Nope..."
>
>------------------------------------------------------------------------------
>_______________________________________________
>qooxdoo-devel mailing list
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel



------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: How to catch exception from asynchronous function?

Tom Saddul
This post has NOT been accepted by the mailing list yet.
This post was updated on .
In reply to this post by voger
My approach to this would be to take both validation in one shot at the server side.  The response of the server which is from an async request would have the right message to be displayed or a callback.  Of course, simple client side validations like checking for empty entries or invalid email format are performed before sending.

There are times that a validation of a control is async (i.e. from some server-side logic), my approach would be to disable the "Send" button (and have a "Confirm" checkbox) or simply have the attached listener check for a confirmed value/s (initially set to "invalid") synchronously so they are invalid until proven valid asynchronously.
Reply | Threaded
Open this post in threaded view
|

Re: How to catch exception from asynchronous function?

voger
In reply to this post by John Spackman-3
Thank you very much. Your code was exactly what I needed. With your help
the complete working example becomes this: http://tinyurl.com/qxvp8vo



On 24/08/2015 06:56 πμ, John Spackman wrote:

> For your first example you have implemented validateUsername as an asynchronous function but asynchronous validations need to be implemented via qx.ui.form.AsyncValidator.  To handle the asynchronous exception, you need to catch it in an AsyncValidator’s callback.   You’re already doing this for the server based test, so perhaps you should merge your two async tests into one, or make validateUsername synchronous (EG because it only runs on the client).
>
> In your second example, you’re getting “you can send” then “nope” because your Async validator calls setValid(true) and then later on setValid(false), so you get two “complete" events.  Also, as with your first example, your validateUsername() needs to either not throw any exceptions (because it is asynchronous) or needs to be called synchronously.
>
> Try this: http://tinyurl.com/ou2cml2
>
>
>
> On 23/08/2015 23:08, "voger" <[hidden email]> wrote:
>
>> I have this example http://tinyurl.com/nay2rnr
>> I am trying to validate asynchronously the username. So on changeValue
>> of that field I call a function that asynchronously validates the
>> username and if it is invalid it throws an error. Unfortunately since
>> the call is asynchronous, when the function returns the caller has long
>> ended so there is no one to catch the exception. How can I handle the
>> exception?
>>
>> I know I can handle the valid state of the item right there in the async
>> function but I want to know what is the proper way to handle
>> asynchronously the exception.
>>
>> Also how can I use that function in a DRY way? The closer I came up is
>> this http://tinyurl.com/o2r56zv but when I press send, even if the field
>> is invalid I get "You can send" first and later I get "Nope..."
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> qooxdoo-devel mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>


------------------------------------------------------------------------------
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel