简体   繁体   English

Javascript有望深入堆栈

[英]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. 为了在管道中可能有1个ajax请求而做出所有这些使用链接的承诺,这感觉太过分了,尤其是因为这种情况很少见。 The other alternative is to just make a synchronous AJAX request. 另一种选择是仅发出同步AJAX请求。

Are my only options to make everything use promises and whens, or to make it a synchronous AJAX request? 我唯一的选择是使所有内容都使用promises和whens,或使其成为同步AJAX请求吗? Is there a nicer alternative? 有更好的选择吗? I'm thinking of C#'s await keyword, but I'm sure other languages have similar functionality. 我在考虑C#的await关键字,但是我确定其他语言也具有类似的功能。

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. 除了AJAX请求之外,还有更多的异步源,我真的想不到在Java中需要同步返回值的情况。

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). 可能异步的事物必须始终返回promise,并且必须始终被消费,就像它是异步的一样(承诺仍会强制执行)。

Is the only other alternative to just make a synchronous AJAX request? 是仅发出同步AJAX请求的唯一其他选择吗?

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. 我在考虑C#的await关键字,但是我确定其他语言也具有类似的功能。

In fact, async-await is coming to ECMAScript as well. 实际上, 异步等待也正在ECMAScript中出现。 But it doesn't make anything synchronous, it rather is just syntactic sugar for promises-everywhere. 但这并不能使任何事情同步,而只是到处实现诺言的语法糖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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