简体   繁体   中英

Javascript promises deep in the stack

I decided to have a fallback call to the server when creating a model in case we can't find a value from the information available locally. I want to have it async if possible, does it mean I have to make everything in this chain of code consume and return promises?

I'm going to use pseudo code to illustrate this point initially, as the call stack can be a little long and so we don't include things that aren't central to this question. Let me know if you need more to 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) {
    // just to illustrate that there's several entry points to this kind of functionality
    var model = this.findOrCreateDocument(options);
}

documentCollection.findOrCreateDocument(options) {
    if (!options.type)
        options.type = resolveType(options);
    // other things that use the found options.type
    return model;
}

resolveType(options) {
    var locallyResolved = resolveLocally(options);
    if (locallyResolved)
        return locallyResolved;
    // try the server as a fallback
    var serverResolved = resolveFromServer(options.id);
    return serverResolved;
}

resolveLocally(options) {
    return stuff;
}

resolveFromServer(id) {
    return ajaxRequestResult();
}

Obviously the actual code is a bit longer and actually does stuff.

It feels excessive to make all of this use chaining promises for the sake of 1 possible ajax request down the pipe, especially as this will be a rare occurrence. The other alternative is to just make a synchronous AJAX request.

Are my only options to make everything use promises and whens, or to make it a synchronous AJAX request? Is there a nicer alternative? I'm thinking of C#'s await keyword, but I'm sure other languages have similar functionality.

Once you start with promises they do spread around, no way around it really since once you go async you must either wait (not feasible) or pass callbacks (which promises solve).

My approach is to embrace them, you'll be more often returning promises then immediate values, but you'll always take care of handling success/error and have a consistent way of dealing with data flow.

There are many more sources of asynchronicity than AJAX requests and I really can't think of a case where I would require a synchronous return value in Javascript.

I want to have it async if possible, does it mean I have to make everything in this chain of code consume and return promises?

Yes. Something that is potentially asynchronous must always return a promise, and must always be consumed as if it was asynchronous (promises enforce this anyway).

Is the only other alternative to just make a synchronous AJAX request?

Yes, and you really don't want that.

Is there a nicer alternative? I'm thinking of C#'s await keyword, but I'm sure other languages have similar functionality.

In fact, async-await is coming to ECMAScript as well. But it doesn't make anything synchronous, it rather is just syntactic sugar for promises-everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM