Skip to content

Instantly share code, notes, and snippets.

@ichtrojan
Forked from joepie91/asynchronous.js
Created April 10, 2018 10:30
Show Gist options
  • Select an option

  • Save ichtrojan/a4f67fa09f242755f1d806be7b584291 to your computer and use it in GitHub Desktop.

Select an option

Save ichtrojan/a4f67fa09f242755f1d806be7b584291 to your computer and use it in GitHub Desktop.

Revisions

  1. @joepie91 joepie91 revised this gist Jan 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion asynchronous.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ hypotheticalFileGetContents("sample.txt", function(fileContents){
    // You've now told it to start the first read, but it won't 'block' your script execution. It will do the read in the background, and immediately move on with the rest of your code.

    console.log("Before the second file is read.");
    hypotheticalFileGetContents("sample.txt", function(fileContents){
    hypotheticalFileGetContents("sample2.txt", function(fileContents){
    // fileContents now contains the file contents, this function is only called when the file read in the background has finished
    console.log("After the second file has completed reading.");
    });
  2. @joepie91 joepie91 created this gist Jan 13, 2015.
    20 changes: 20 additions & 0 deletions asynchronous.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    console.log("Before the first file is read.");
    hypotheticalFileGetContents("sample.txt", function(fileContents){
    // fileContents now contains the file contents, this function is only called when the file read in the background has finished
    console.log("After the first file has completed reading.");
    });

    // You've now told it to start the first read, but it won't 'block' your script execution. It will do the read in the background, and immediately move on with the rest of your code.

    console.log("Before the second file is read.");
    hypotheticalFileGetContents("sample.txt", function(fileContents){
    // fileContents now contains the file contents, this function is only called when the file read in the background has finished
    console.log("After the second file has completed reading.");
    });

    /* Output could look something like this:
    Before the first file is read.
    Before the second file is read.
    After the first file has completed reading.
    After the second file has completed reading.
    */
    17 changes: 17 additions & 0 deletions synchronous.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?php

    echo("Before the first file is read.");
    $fileContents = file_get_contents("sample.txt"); // You can't do anything while the file is being read, your script is 'stuck'.
    echo("After the first file has completed reading.");

    echo("Before the second file is read.");
    $fileContents = file_get_contents("sample2.txt"); // You can't do anything while the file is being read, your script is 'stuck'.
    echo("After the second file has completed reading.");


    /* Output always looks like this:
    Before the first file is read.
    After the first file has completed reading.
    Before the second file is read.
    After the second file has completed reading.
    */