Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including synch-steps with all npm packages installed. Try it out:

var synchSteps = require("synch-steps")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

synch-steps v1.1.0

Callback hell alternative! Synchronously execute steps.

synch-steps

Callback hell alternative! Synchronously execute steps.

Usage

var SynchSteps = require('synch-steps');
var fs = require('fs');

var fooBarSteps = new SynchSteps();

fooBarSteps.step(function(next) {
   var foo = 10;
   var bar = 20;
   fs.readFile('/foo', function(err, result) {
      if (err) {
         // handle err
      } else {
         // process the file contents
      }
      next();
   });
}).step(function(next) {
   // This step executes after the previous step is done executing
   var baz = 20;
   fs.readFile('/bar', function(err, result) {
      if (err) {
         // handle err
      } else {
         // process the file contents
      }
      next();
   });
}).step(function(next) {
   // This is the last step in this example:
   // This step executes after the previous step is done executing
   next();
});

fooBarSteps.execute(function() {
   console.log('All steps are now done executing.');
});

You can choose to stop the execution of further statements by calling the stop() function as follows:

var SynchSteps = require('synch-steps');
var assert = require('assert');

var testString = '';

steps = new SynchSteps();

steps.step(function(next) {
   testString += 'x';
   next();
}).step(function(next, stop) {
   testString += 'y';
   stop();
}).step(function(next) {
   testString += 'z';
   next();
});

steps.execute(function() {
   assert.equal(testString, 'xy');
});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free