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:
- how detect situation , handle in case of successful error response?
- a promise pattern useful
- 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?
- this falls bit out of scope. suggest asking in separate question.
here library references well:
- q: https://github.com/kriskowal/q
- rsvp: https://github.com/tildeio/rsvp.js
- winjs: http://msdn.microsoft.com/en-us/library/windows/apps/br211867.aspx
i not suggesting use of these. that's discover. hopefully, can see pattern, however, , allow decide yourself.
Comments
Post a Comment