Last active
July 21, 2016 15:27
-
-
Save ratopi/9f184eb5086c2670f3c9d03c398477ec to your computer and use it in GitHub Desktop.
Asynchronous JavaScript <-> Console Communication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var factory = function ( process ) | |
{ | |
var currentReceiverFun = []; | |
var receiverFun = function ( data ) | |
{ | |
if ( currentReceiverFun.length > 0 ) | |
{ | |
var fn = currentReceiverFun.unshift(); | |
fn( data ); | |
} | |
}; | |
var sendLine = function ( data, fn ) | |
{ | |
currentReceiverFun.push( fn ); | |
process.stdin.write( data ); | |
}; | |
process.stdout.on( 'data', receiverFun ); | |
return sendLine; | |
}; | |
// --- | |
var myHandler = factory(); | |
myHandler( | |
"Hallo, Welt!", | |
function ( line ) | |
{ | |
console.log( "received " + line ); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment