Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Combining sequences with asynquence - https://github.com/getify/asynquence
- //
- // This code was prepared as an answer to https://github.com/getify/asynquence/issues/84
- // It is a skeleton of working code that shows how to make various sequences interact.
- //
- // There are four functions:
- // - DoSomethingUseful() gets called when it's time to update some data structure.
- // To make its update, it calls SendCommand() to request some data, which it then uses.
- // - SendCommand() is also a sequence. It calls Connect() to establish a connection
- // then sends a command and waits for the response to return.
- // - Connect() is another sequence. It checks to see if a connection has already been
- // established, in which case, it returns to complete the caller's step.
- // Otherwise, it establishes the connection and then returns/completes the step
- // - tlsConnect() shows how to handle a non error-first callback, and also provides
- // an example of using the node tls connection with asynquence
- //
- // NOTE: THIS CODE DOES NOT WORK. It is only schematic, but gives the flavor of using asynquence
- // 16 Feb 2016 - www.richb-hanover.com
- // ===========================================================================
- ASQ = require("asynquence");
- /*
- * DoSomethingUseful() is a sequence that calls SendCommand() to
- * send a request and return its response. It uses that response to do
- * something useful in the program.
- */
- function DoSomethingUseful() {
- ASQ() // create an empty sequence
- .then((done) => {
- SendCommand(done, "ThisIsACommand"); // SendCommand() calls the done trigger when it completes
- })
- .then((done, response) => { // response comes from SendCommand()'s response
- //... some code to use 'response' ... // use the response in some useful way
- done(); // call done() to indicate the end of DoSomethingUseful()
- })
- .or((err) => { // or handle the error
- console.log(`Yikes! Something bad happened: ${err}`);
- })
- }
- /*
- * SendCommand(fndone, cmd) - starts a sequence, sends a command and waits for the response
- * fndone is the done trigger that indicates SendCommand() has completed
- */
- function SendCommand(fndone, cmd) { // fndone is the trigger that completes the *caller's* step
- var eventName;
- ASQ() // Create an empty sequence
- .then((done) => {
- Connect(done) // Call Connect() - when it finishes, it calls done()
- }) // to move to the next step of this sequence
- .then((done) => {
- writeCommand(cmd, svr); // Now that connection is established, write command to other end
- EventEmitter.once(eventName,(resp) => { // In this example, an 'eventName' event indicates a response has arrived
- done(resp); // done(resp) indicates the end of *this step* and passes on the response
- });
- })
- .then((done, resp) => { // resp is the response from previous step
- fndone(resp) // fndone(resp) returns the response and
- }) // indicates that the *function* is done
- .or((err) => {
- console.log(`Yikes! Command error '${err}'`); // .or(err) handles error cases...
- });
- }
- /*
- * Connect(fndone) - establishes a connection.
- * The first step of the sequence checks to see if the connection is already established
- * If so, it calls fndone() and returns to its caller
- * Otherwise, it goes through the steps to establish the connection, then calls fndone()
- */
- function Connect(fndone) { // fndone is the trigger that completes the *caller's* step
- ASQ() // Create an empty sequence
- .then((done) => { // First step - check to see if we're already connected
- if (isConnected()) {
- fndone(svr); // if so, call the *caller's* done trigger
- }
- else {
- done(); // otherwise, call the local trigger to end this step
- }
- })
- .then((done) => { // next step - tlsConnect sets up a connection
- tlsConnect(done) // it calls done(svr) to return the connection info
- })
- .then((done, svr) => { // next step - createStream receives svr from previous step
- createStream(done, svr) // it calls done() when complete (no info passed to next step)
- })
- .then((done) => { // next step - log the info
- console.log( "Ended Connect - streams created/connected");
- fndone(svr); // and call the *caller's* trigger - fndone()
- })
- .or((err) => { // or() handles errors
- console.log( "Ended Connect with error: ", err.message);
- });
- }
- var tls = require('tls'); // example of non-error-first callback (not an errfcb)
- function tlsConnect(done) { // non-ASQ code
- //...
- var svr = tls.connect( // the tls.connect() function takes a port, options, and a callback when complete
- tlsPort, // your port
- tlsOptions, // your options
- () => { // the callback
- svr.on('close', (err) => { // set up stream handlers for close and error
- console.log( "TLS stream was closed by other end.", err);
- });
- svr.on('error', (err) => {
- console.log( "TLS stream got error: ", err);
- });
- done(svr); // call the done(svr) trigger to pass back the tls stream
- }
- )}
- function createSaxStream(done, svr) { // non-ASQ code
- // ...
- done();
- }
Advertisement
Add Comment
Please, Sign In to add comment