Skip to content

Instantly share code, notes, and snippets.

@solanoepalacio
Last active July 17, 2020 02:38
Show Gist options
  • Select an option

  • Save solanoepalacio/26e60253afaf793615ef2ad115a45903 to your computer and use it in GitHub Desktop.

Select an option

Save solanoepalacio/26e60253afaf793615ef2ad115a45903 to your computer and use it in GitHub Desktop.

Revisions

  1. solanoepalacio revised this gist Jul 17, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions delay-el.js
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,9 @@ new Promise((resolve) => {
    1 + 1 // nothing, really...
    }
    });

    let timeoutScheduled = Date.now();

    setTimeout(() => {
    const delay = Date.now() - timeoutScheduled;

  2. solanoepalacio revised this gist Jul 17, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions delay-el.js
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,11 @@ const { spawn } = require('child_process');

    // Create a task that takes 95ms to complete
    new Promise((resolve) => {

    console.time('sleep');
    spawn('sleep', ['0.085']).on('close', () => {
    console.log('closed');
    console.timeEnd('sleep');

    resolve();
    });
    }).then(() => {
    @@ -27,7 +27,6 @@ new Promise((resolve) => {
    });
    let timeoutScheduled = Date.now();
    setTimeout(() => {

    const delay = Date.now() - timeoutScheduled;

    console.log(`${delay} ms have passed since I was scheduled`);
  3. solanoepalacio created this gist Jul 17, 2020.
    36 changes: 36 additions & 0 deletions delay-el.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    /**
    * Shows how the event loop can be delayed to the execution of a timer.
    * inspired on node-js docs example: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
    */

    'use-strict';

    const { spawn } = require('child_process');

    // Create a task that takes 95ms to complete
    new Promise((resolve) => {

    console.time('sleep');
    spawn('sleep', ['0.085']).on('close', () => {
    console.log('closed');
    console.timeEnd('sleep');
    resolve();
    });
    }).then(() => {
    console.log('then');
    // block the thread for 10s
    const startCallback = Date.now();

    while (Date.now() - startCallback < 10) {
    1 + 1 // nothing, really...
    }
    });
    let timeoutScheduled = Date.now();
    setTimeout(() => {

    const delay = Date.now() - timeoutScheduled;

    console.log(`${delay} ms have passed since I was scheduled`);
    }, 100);

    'done'