简体   繁体   English

防止返回直到满足条件

[英]Prevent return until condition is met

I know these types of question come up fairly often, but I need help with a wait-like mechanism in JavaScript. 我知道这些类型的问题经常出现,但我需要帮助JavaScript中的等待机制。 I know setTimeout-based solutions are going to come up, but I'm not sure how to pull it off in my case. 我知道基于setTimeout的解决方案会出现,但我不知道如何在我的情况下解决它。

I'm writing an API that uses a WebSocket internally. 我正在编写一个内部使用WebSocket的API。 There's a connect() method that sets up the WebSocket, and I need to make it not return until after the WebSocket is set up. 有一个设置WebSocket的connect()方法,我需要让它在WebSocket建立之后才返回。 I'd like it to return a value for whether or not the connection was successful, but that's not the main problem. 我希望它返回连接是否成功的值,但这不是主要问题。

The issue I'm hitting is that after a user calls connect(), they may call another method that relies on the WebSocket to be properly set up. 我遇到的问题是,在用户调用connect()之后,他们可能会调用另一个依赖于WebSocket的方法来正确设置。 If it's called too early, an error is thrown stating that the object is not usable. 如果过早调用,则会抛出错误,指出该对象不可用。

My current solution is setting a "connected" flag when I've determined a successful connection and in each method checking for it in each method. 我当前的解决方案是在确定连接成功时设置“连接”标志,并在每种方法中检查每种方法。 If it's not connected, I add the method call to a queue that is ran through by the same code that sets the flag. 如果它没有连接,我将方法调用添加到由设置标志的相同代码运行的队列。 This works, but it introduces that style of code all over my methods and also seems misleading from the user-perspective, since the call of those functions is deferred. 这是有效的,但它在我的方法中引入了这种代码风格,并且从用户角度来看似乎也有误导性,因为这些函数的调用是延迟的。 Also, if there is other user code that relies on those calls being completed before it gets to them, it won't behave as expected. 此外,如果有其他用户代码依赖于那些调用在它们到达之前完成,则它将不会按预期运行。

I've been racking my brain with how to handle this case. 我一直在绞尽脑汁处理这个案子。 The easiest solution is to just find a way to block returning from connect until after the WebSocket is set up, but that's not really the JavaScript way. 最简单的解决方案是找到阻止从连接返回的方法,直到建立WebSocket之后,但这不是JavaScript方式。 The other option was to make them provide the rest of their code in a callback, but that seems like a weird thing to do in this case. 另一种选择是让它们在回调中提供其余的代码,但在这种情况下,这似乎是一件奇怪的事情。 Maybe I'm over-thinking it? 也许我过度思考了?

Edit: To better illustrate my problem, here's a example of what the user could do: 编辑:为了更好地说明我的问题,这里有一个用户可以做的例子:

var client = new Client(options);
client.connect();
client.getServerStatus();

The getServerStatus() method would be using the WebSocket internally. getServerStatus()方法将在内部使用WebSocket。 If the WebSocket is not set up yet, the user will get that not usable error. 如果尚未设置WebSocket,则用户将获得不可用的错误。

Todays Javascript does not really work like that unfortunately. 不幸的是,今天的Javascript并没有真正起作用。 In the future (ECMA6) there may be new language features that address this issue more directly. 将来(ECMA6)可能会有更新的语言功能更直接地解决这个问题。 However for now you are stuck with the currently accepted method of handling asynchronous events, which is limited to callbacks. 但是现在你仍然坚持使用当前接受的处理异步事件的方法,这种方法仅限于回调。 You may also want to explore 'promises' to handle 'callback hell' however you will need a library for this. 您可能还想探索'promises'来处理'回调地狱',但是你需要一个库来实现这个目标。

And yes it does seem strange to have callbacks everywhere, especially for someone new to web programming, however it is really the only way to go about it at this stage (assuming you want a cross-browser friendly solution). 是的,在任何地方都有回调似乎很奇怪,特别是对于刚接触网络编程的人来说,但是在这个阶段它真的是唯一的方法(假设你想要一个跨浏览器友好的解决方案)。

"Wait" is almost the keyword you are looking for. “等待”几乎是您正在寻找的关键字。 Actually, it's yield that does this. 实际上,这是yield See eg MDN's documentation . 请参阅MDN的文档

There's a connect() method that sets up the WebSocket, and I need to make it not return until after the WebSocket is set up 有一个connect()方法设置WebSocket,我需要让它在WebSocket建立之后才返回

That isn't going to happen unless you rewrite the javascript execution engine. 除非你重写javascript执行引擎,否则不会发生这种情况。

Either the code trying to send data will need to check the socket state (I'd go with encapsulating the socket in a object, supplying a method which sets a member variable on the open/close events and poll the state of that member variable from the external code). 尝试发送数据的代码需要检查套接字状态(我将封装在一个对象中,提供一个方法,在open / close事件上设置一个成员变量,并从中调查该成员变量的状态)外部代码)。 Alternatively you could add messages and call backs to a queue and process the queue when the socket connects. 或者,您可以向队列添加消息和回调,并在套接字连接时处理队列。

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

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