javascript - Typical approach to handling late asynchronous callbacks -


i'm new web application development using javascript.

as javascript applications appear work highly asynchronous i'm wondering if there common approach handling callbacks invoked late. example assuming asynchronous request made , when response comes application in different state not suitable processing response.

how detect situation , handle in case of successful error response? can issue solved high-level architectural view or kind of solution tied js framework and/or type of application?

in related matter recommended pattern web applications block input while server-side processing takes place?

thank you

the quick answer question promise.

promises pattern may implemented ecma-6, exercised through own custom code or use of libraries such kris kowal's q (see below).

the pattern simple. goal wrap asynchronously-executing method or function in order preserve it's scope , act on it's return. libraries , approaches referred statement.

a simple example:

var theeventualresults;  gogetmesomething().then(function(results) {   //now can store results in working scope   theeventualresults = results; }); 

the idea gogetmesomething() returns "promise object" has method then executes when gogetmesomething executes. there ways implement failure, through catching errors or reading failure response codes requests.

a failure example:

var theeventualresults;  gogetmesomething().then(function(results) {   //now can store results in working scope   theeventualresults = results; }).error(function(reason) {   //now can handle failure, if fails; }); 

i suggest conduct little research on promises , different implementations in different libraries, further grasp. find what's best specific application.

so, in review of questions:

  1. how detect situation , handle in case of successful error response?
    • a promise pattern useful
  2. can issue solved high-level architectural view or kind of solution tied js framework and/or type of application?
    • this kind of solution can implemented in javascript independently of framework. however, if using framework, may have promise pattern. typically useful implement framework offers rather using else. angularjs example of alex c pointed out.
  3. in related matter recommended pattern web applications block input while server-side processing takes place?
    • this falls bit out of scope. suggest asking in separate question.

here library references well:

i not suggesting use of these. that's discover. hopefully, can see pattern, however, , allow decide yourself.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -