Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>Asynchronous Vars</h2>
<i>(From a discussion about debouncing)</i></br>
</br>
<i>(View in your web inspector)</i>
</br>
</br>
<div id="here"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const print = str => {
  const el = document.getElementById("here");
  const txt = document.createTextNode(str);
  el.append(txt);
  const br = document.createElement("br");
  el.append(br);
  console.log(str)
};

/**
 * First example.
 */

const firstExample = () => {
  let i;
  for (i = 0; i < 10; i++) {
    print("1st middle: " + i);
  }
  print("1st end: " + i);
};
firstExample();

/**
 * Second example.
 */

const secondExample = () => {
  let i;
  for (i = 0; i < 10; i++) {
    print("2nd middle: " + i);
  }
  print("2nd end: " + i);
  return i;
};
let j = 0;
j = secondExample();
print("J 2nd: " + j);

/**
 * Third example - this is the one that will behave errantly.
 *
 * Timeouts, in general, require a promise to wrap it - to ensure
 * that the resulting code is executed after the main logic is complete.
 *
 * More troubling, however, is when the asynchronous logic breaks or stops
 * between the completion of the timeout - i.e. during a long API call that
 * fails.
 *
 * debouncing is an important technique required here.
 *
 * In the past, we ensured that active operations were limited to a small
 * subset of tables. We also ensured that active read tables were small
 * and denormalized.
 *
 * Still, debouncing is a required approach for certain operations. Consider one
 * that must return part of a lengthy call or not. Previously, we built API's where
 * errors with the API would fail (rolling back) or succeed (committing). Data would
 * be collated into an object and returned if the remainder of the operations failed.
 *
 * This point is partly moot now (all of the above is also) with the greater abundance
 * of streams in managing client-side data. Still, releveant and worthwhile to review!
 */

const thirdExample = () => {
  return setTimeout(() => {
    let y = 0;
    for (let i = 0; i < 10; i++) {
      y++;
    }
    print("3rd end: " + y);
    return y;
  }, 1000);
};
let z = 0;
z = thirdExample();
print("Z 3rd: " + z);

/**
 * Z 3rd: 1
 * 3rd end: 10
 */

// Note that setTimeout() forces a pause before executing the logic.
/**
 * Fourth example.
 */

const fourthExample = () => {
  let y = 0;
  for (let i = 0; i < 10; i++) {
    y++;
    print("4th middle: " + y);
  }
  print("4th end: " + y);
  return y;
};

let v = 0;
v = fourthExample();
print("V 4th: " + v);

/**
 * Fifth example.
 */

const fifthExample = () => {
  let y = 0;
  setTimeout(() => {
    y = 10000
  }, 4000)
  return y
};

print("5th: " + fifthExample())
              
            
!
999px

Console