Javascript Socket

28 views
Skip to first unread message

yingi keme

unread,
Jul 31, 2017, 3:30:29 PM7/31/17
to d3-js
Is there a way to pause execution of code in a javascript file until a socket connection is established or until it gets socket onmessage is called.

Marc Fawzi

unread,
Jul 31, 2017, 3:44:11 PM7/31/17
to d3...@googlegroups.com
onmessage is itself an event, so execution of its handler does not occur until a message is received. 

On Mon, Jul 31, 2017 at 12:30 PM, yingi keme <ying...@gmail.com> wrote:
Is there a way to pause execution of code in a javascript file until a socket connection is established or until it gets socket onmessage is called.

--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yingi keme

unread,
Jul 31, 2017, 4:01:09 PM7/31/17
to d3...@googlegroups.com
Yea. But other codes in the script goes ahead with the execution while waiting for the event onmessage. How will you pause all other lines till onmessage is called?

Yingi Kem
You received this message because you are subscribed to a topic in the Google Groups "d3-js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/d3-js/Wo1eqTa9C1A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to d3-js+un...@googlegroups.com.

Marc Fawzi

unread,
Jul 31, 2017, 5:02:47 PM7/31/17
to d3...@googlegroups.com
build an implicit state machine via coroutines (based on es6 generators) and yield (asynchronously in a while loop) till the side effect form onmessage is materialized then kick the coroutine with an event to some specific state where it execute the otherwise-idle part of the code. There are so many ways to do this from a control flow perspective, and then there is Observables (reactive dataflow) 

so depending on whether you want to do it with something established like RxJS (reactive dataflow) or invent your own model on top of Javascript's cooperative multitasking (aka concurrency) primitives ...

if stuff sounds foreign, I would advise going the simpler RxJS route.


To unsubscribe from this group and all its topics, send an email to d3-js+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Curran

unread,
Aug 1, 2017, 9:16:38 AM8/1/17
to d3-js
I bet there's a simple solution using some sort of asynchronous control flow. The code that needs to wait for the message can be wrapped in a function that gets invoked in response to the message event.

Yingi, do you have an runnable code example you could share that demonstrates the problem? It's hard to say in detail without seeing any example.

Marc Fawzi

unread,
Aug 1, 2017, 11:55:37 AM8/1/17
to d3...@googlegroups.com
Yes but I imagined by "code in JS file" he might have meant it generically, not one specific block of code, and my first thought for async control flow was state machines...

Sure, I suspect the problem is more confined/narrowly defined and not so generic, but you never know...

Sent from my iPhone

On Aug 1, 2017, at 6:16 AM, Curran <curran....@gmail.com> wrote:

I bet there's a simple solution using some sort of asynchronous control flow. The code that needs to wait for the message can be wrapped in a function that gets invoked in response to the message event.

Yingi, do you have an runnable code example you could share that demonstrates the problem? It's hard to say in detail without seeing any example.

--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+un...@googlegroups.com.

yingi keme

unread,
Aug 1, 2017, 12:38:32 PM8/1/17
to d3...@googlegroups.com
Thanks Marc.

And yes i was actually relating to a block of code. I think Curran explanation made it clearer to me because i am quite naive with my javascript skills.

Thanks Curran..!!!! Thanks Marc..!!!!!!!

Yingi Kem
You received this message because you are subscribed to a topic in the Google Groups "d3-js" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/d3-js/Wo1eqTa9C1A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to d3-js+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages