简体   繁体   English

在Node.js中使用上下文远程调用函数

[英]Call function with context remotely in Node.js

I am trying to write a javascript program which executes series of functions sequentially (say function1, function2 ... function5). 我正在尝试编写一个JavaScript程序,该程序顺序执行一系列功能(例如function1,function2 ... function5)。 I want the execution of these functions to continue irrespective of the success or failure of the previous functions, ie function1 returns success, function2 returns success, function3 returns error (but the execution does not stop here), function4 returns success and functio5 returns success. 我希望这些功能的执行能够继续进行,而与先前功能的成功或失败无关,即function1返回成功,function2返回成功,function3返回错误(但执行不在此处停止),function4返回成功,functio5返回成功。

Now when one of the functions returns error, I want to retry execution of that function separately. 现在,当其中一个函数返回错误时,我想分别重试该函数的执行。 In the above example, I want to try to re-execute function3 (the input and output of function3 are independent of other functions). 在上面的示例中,我想尝试重新执行function3(function3的输入和输出独立于其他函数)。 But I want the module where functions 1-5 are running different from the module which does the re-execution. 但是我想要运行功能1-5的模块与执行重新执行的模块不同。 So I need a way to execute functions in on module through another module. 因此,我需要一种通过另一个模块在模块上执行功能的方法。 So I have two questions now, 所以我现在有两个问题,

1. Is there an error handing library in javascript to do this? 1. javascript中是否有错误处理库?

2. If not, how do I pass the context/state of the documents/objects in one module to a function in the different module (say, error recovery module) for re-execution ? 2.如果没有,如何将一个模块中文档/对象的上下文/状态传递给另一个模块中的功能(例如,错误恢复模块)以重新执行?
PS: I am relatively new to javascript, hence I do not know if any pattern exists to deal with such scenario. PS:我对javascript比较陌生,因此我不知道是否存在任何模式可以处理这种情况。 In Java, I assume, you would serialize the object, spawn a different thread, use RMI to send the context and re-execute the function remotely. 在Java中,我假设您将序列化对象,生成另一个线程,使用RMI发送上下文并远程重新执行该函数。

Thank you. 谢谢。

There are a lot of different approaches here, depending on the details. 根据细节,这里有很多不同的方法。

As for the error retrying, just pass the function to a module like attempt . 至于错误重试,只是传递的功能就像一个模块的尝试 Or make it a promise-based pattern so on the failed promise, retry the current function. 或者将其设为基于承诺的模式,以便对失败的承诺进行重试,然后重试当前功能。

Other stuff, depends on your details. 其他东西,取决于您的详细信息。

Do you want to pass the same argument to all of the functions? 是否要将相同的参数传递给所有函数? It looks like it, since you want to retry step number 3 regardless of others. 看起来像这样,因为无论其他情况您都想重试第3步。 You can simply use async . 您可以简单地使用async

function first() {
    // do stuff
    return 1;
}
function second() {
    // do stuff
    return 2;
}
// ... up to fifth

async.series([first, second, third, fourth, fifth], function (err, results) {
    console.log('Results: ', results); // should return [1, 2, 3, 4, 5] 
});

Or maybe you want a waterfall, pass results of the previous function to the next? 或者,也许您想要瀑布,将上一个函数的结果传递给下一个? Then you'd use something like async-waterfall . 然后,您将使用async-waterfall之类的东西。

Do you want to store success or not? 您是否要存储成功? Globally? 在全球范围内? Locally? 本地? What about number of retries? 重试次数呢?

This is the best I can do without further info. 如果没有更多信息,这是我能做的最好的事情。

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

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