Skip to content

Instantly share code, notes, and snippets.

@ratopi
Last active July 21, 2016 15:27
Show Gist options
  • Save ratopi/9f184eb5086c2670f3c9d03c398477ec to your computer and use it in GitHub Desktop.
Save ratopi/9f184eb5086c2670f3c9d03c398477ec to your computer and use it in GitHub Desktop.
Asynchronous JavaScript <-> Console Communication
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