在NodeJS中实现阻塞控制流程 [英] Implement blocking control flow in NodeJS

查看:134
本文介绍了在NodeJS中实现阻塞控制流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个阻塞控制流程,但已经走到了尽头。
这个想法是一个请求进来并流经许多处理函数。每个功能都会执行某些操作,并决定是否现在满足请求。如果是,则处理停止,否则,调用下一个处理程序。
每个处理程序可能会也可能不会异步执行某些操作(如获取/写入数据)。如果是这种情况,那么以下处理程序依赖于启动之前的数据。

I am trying to create a blocking control flow, but have hit a dead end. The idea is that a request comes in and flows through a number of handler functions. Each function does something and decides whether or not the request is now fulfilled. If it is, the handling stops, if not, the next handler is invoked. Each handler might or might not do something asynchronously (like fetching/writing data). If that is the case, then the following handlers depend on the data being there before being started.

到目前为止,我有两个想法,它们都不匹配这些要求:
1)所有处理程序都是函数,它们被推入一个数组并用一些进行迭代。如果处理程序希望停止控制流,则只需返回 true
使用这种方法,我不能让任何处理程序调用异步函数。

So far, I have two ideas, which both do not quite match these requirements: 1) All handlers are functions, which are pushed into an array and iterated over with some . If a handler wishes to stop the control flow, it simply needs to return true. With this approach, I can't have any of the handlers invoking asynchronous functions.

2)所有处理程序都是链接的promise。这对我来说似乎是一个更好的主意,但我无法弄清楚如何最好地处理停止控制流程。我有两个想法:
- 一旦处理程序决定中断流程,保留一个设置为 true 的变量。在每个处理程序中检查此值,并在需要时立即解决。这意味着很多重复的代码。
- 拒绝承诺,如果它不希望继续正常流程,并继续 catch 。但是,使用错误作为控制流程的一种手段的想法让我很痛苦 - 这也意味着我必须处理由于实际错误而调用catch的情况。

2) All handlers are promises which are chained. This seems like a better idea to me, but I can't figure out how to best handle stopping the control flow. I have two ideas: - Keep a variable which is set to true once a handler decides to interrupt the flow. Check this value in every single handler and resolve immediately, if needed. This means a lot of duplicate code. - Rejecting a promise if it does not wish to continue the normal flow, and continuing in catch. However, the thought of using errors as a means of controlling the flow pains me - and also it means that I have to handle the case where the catch is invoked because of an actual error.

我的蜘蛛般的感觉告诉我必须有另一种方式,但我无法弄明白。

My spidey senses tell me there has to be another way, but I can't figure it out.

推荐答案

不要链接承诺处理程序,嵌套它们。您可以通过一系列函数以编程方式执行此操作:

Don't chain the promise handlers, nest them. You can do that programmatically from an array of functions:

var handlers = […]; // functions that can return promises

var run = handlers.reduceRight(function(next, handler) {
    return function() {
        return Promise.resolve(someArg).then(handler).then(function(result) {
            if (result) // true, whatever
                return result;
            else
                return next();
        });
    };
}, function end() {
    return Promise.reject(new Error("no handler matched"));
});

run().then(function(result) {
    // the result from the first handler that returned anything
}, function(err) {
    // some handler threw an exception, or there was no handler
});

这篇关于在NodeJS中实现阻塞控制流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆