Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在es6中,将带有回调的事件侦听器设置为iterable_Javascript_Iterator_Listener_Generator - Fatal编程技术网

Javascript 在es6中,将带有回调的事件侦听器设置为iterable

Javascript 在es6中,将带有回调的事件侦听器设置为iterable,javascript,iterator,listener,generator,Javascript,Iterator,Listener,Generator,我的websocket类,Wws,使用onmessage侦听所有传入的消息 对于使用我的websocket模块Wws的程序,我希望它们通过iterables“侦听” 比如说, // listen for new blog messages let itr = ws.listen( 'blog', 'new' ); // display each new blog as we receive it while( true ) { let blog = itr.next().value;

我的websocket类,
Wws
,使用
onmessage
侦听所有传入的消息

对于使用我的websocket模块
Wws
的程序,我希望它们通过
iterable
s“侦听”

比如说,

// listen for new blog messages let itr = ws.listen( 'blog', 'new' ); // display each new blog as we receive it while( true ) { let blog = itr.next().value; dispBlog( blog ); }
如何将带有
回调的
侦听器
转换为
迭代器

for…of
正在阻塞。只要您处于该循环中,它就会阻止服务器执行任何其他操作,包括接受新请求。“你不想那样做的。”托菲-谢谢-不知道。更改为
then()
ws = function() { conn = new Websocket(...); // could have global `onmessage` that passes through the messages conn.onmessage = function(msg) { // can look at listeners and know there are iterators // but how to talk to them? } listeners = new Map(); // generator for listening to incoming messages listener = function*( service, action ) { // can add this service.action to listeners, but what? // does this guy create his own `onmessage`? // if so, how does he get the `yield` to the outer function? }