Javascript promises deep in the stack -
i decided have fallback call server when creating model in case can't find value information available locally. want have async if possible, mean have make in chain of code consume , return promises?
i'm going use pseudo code illustrate point initially, call stack can little long , don't include things aren't central question. let me know if need more go on though.
function router() { if (condition) { controller.renderdocument(options); } } controller.renderdocument(options) { documentcollection.findorcreatedocumentholder(options); } documentcollection.findorcreatedocumentholder(options) { var model = this.findorcreatedocument(options); } documentcollection.otherfunction(options) { // illustrate there's several entry points kind of functionality var model = this.findorcreatedocument(options); } documentcollection.findorcreatedocument(options) { if (!options.type) options.type = resolvetype(options); // other things use found options.type return model; } resolvetype(options) { var locallyresolved = resolvelocally(options); if (locallyresolved) return locallyresolved; // try server fallback var serverresolved = resolvefromserver(options.id); return serverresolved; } resolvelocally(options) { return stuff; } resolvefromserver(id) { return ajaxrequestresult(); }
obviously actual code bit longer , stuff.
it feels excessive make of use chaining promises sake of 1 possible ajax request down pipe, rare occurrence. other alternative make synchronous ajax request.
are options make use promises , whens, or make synchronous ajax request? there nicer alternative? i'm thinking of c#'s await keyword, i'm sure other languages have similar functionality.
i want have async if possible, mean have make in chain of code consume , return promises?
yes. potentially asynchronous must return promise, , must consumed if asynchronous (promises enforce anyway).
is other alternative make synchronous ajax request?
yes, , don't want that.
is there nicer alternative? i'm thinking of c#'s await keyword, i'm sure other languages have similar functionality.
in fact, async-await coming ecmascript well. doesn't make synchronous, rather syntactic sugar promises-everywhere.
Comments
Post a Comment