Skip to content

Instantly share code, notes, and snippets.

@aponxi
Forked from victorquinn/promise_while_loop.js
Created June 17, 2018 02:58
Show Gist options
  • Select an option

  • Save aponxi/f8207b30d301ce4722788952de2ed593 to your computer and use it in GitHub Desktop.

Select an option

Save aponxi/f8207b30d301ce4722788952de2ed593 to your computer and use it in GitHub Desktop.

Revisions

  1. @victorquinn victorquinn revised this gist Dec 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promise_while_loop.js
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ promiseWhile(function() {
    }, function() {
    // The function to run, should return a promise
    return new Promise(function(resolve, reject) {
    // Arbitrary 25ms async method to simulate async process
    // Arbitrary 250ms async method to simulate async process
    setTimeout(function() {
    sum++;
    // Print out the sum thus far to show progress
  2. @victorquinn victorquinn renamed this gist Dec 19, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @victorquinn victorquinn revised this gist Dec 19, 2013. 2 changed files with 25 additions and 24 deletions.
    26 changes: 25 additions & 1 deletion 1.promise_while_loop.js
    Original file line number Diff line number Diff line change
    @@ -13,4 +13,28 @@ var promiseWhile = function(condition, action) {
    process.nextTick(loop);

    return resolver.promise;
    };
    };


    // And below is a sample usage of this promiseWhile function
    var sum = 0,
    stop = 10;

    promiseWhile(function() {
    // Condition for stopping
    return sum < stop;
    }, function() {
    // The function to run, should return a promise
    return new Promise(function(resolve, reject) {
    // Arbitrary 25ms async method to simulate async process
    setTimeout(function() {
    sum++;
    // Print out the sum thus far to show progress
    console.log(sum);
    resolve();
    }, 250);
    });
    }).then(function() {
    // Notice we can chain it because it's a Promise, this will run after completion of the promiseWhile Promise!
    console.log("Done");
    });
    23 changes: 0 additions & 23 deletions 2.sample_usage.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    // Assuming the above gist and the Bluebird library have been included already of course...

    var sum = 0,
    stop = 10;

    promiseWhile(function() {
    // Condition for stopping
    return sum < stop;
    }, function() {
    // The function to run, should return a promise
    return new Promise(function(resolve, reject) {
    // Arbitrary 25ms async method to simulate async process
    setTimeout(function() {
    sum++;
    // Print out the sum thus far to show progress
    console.log(sum);
    resolve();
    }, 250);
    });
    }).then(function() {
    // Notice we can chain it because it's a Promise, this will run after completion of the promiseWhile Promise!
    console.log("Done");
    });
  4. @victorquinn victorquinn revised this gist Dec 18, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 1.promise_while_loop.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    var Promise = require('bluebird');

    var promiseWhile = function(condition, body) {
    var promiseWhile = function(condition, action) {
    var resolver = Promise.defer();

    var loop = function() {
    if (!condition()) return resolver.resolve();
    return Promise.cast(body())
    return Promise.cast(action())
    .then(loop)
    .catch(resolver.reject);
    };
  5. @victorquinn victorquinn revised this gist Dec 18, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 2.sample_usage.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ promiseWhile(function() {
    // Condition for stopping
    return sum < stop;
    }, function() {
    // The method to run, should return a promise
    // The function to run, should return a promise
    return new Promise(function(resolve, reject) {
    // Arbitrary 25ms async method to simulate async process
    setTimeout(function() {
    @@ -18,5 +18,6 @@ promiseWhile(function() {
    }, 250);
    });
    }).then(function() {
    // Notice we can chain it because it's a Promise, this will run after completion of the promiseWhile Promise!
    console.log("Done");
    });
  6. @victorquinn victorquinn revised this gist Dec 18, 2013. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  7. @victorquinn victorquinn revised this gist Dec 18, 2013. 2 changed files with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions Sample Usage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // Assuming the above gist and the Bluebird library have been included already of course...

    var sum = 0,
    stop = 10;

    promiseWhile(function() {
    // Condition for stopping
    return sum < stop;
    }, function() {
    // The method to run, should return a promise
    return new Promise(function(resolve, reject) {
    // Arbitrary 25ms async method to simulate async process
    setTimeout(function() {
    sum++;
    // Print out the sum thus far to show progress
    console.log(sum);
    resolve();
    }, 250);
    });
    }).then(function() {
    console.log("Done");
    });
    File renamed without changes.
  8. @victorquinn victorquinn renamed this gist Dec 18, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. @victorquinn victorquinn created this gist Dec 18, 2013.
    16 changes: 16 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    var Promise = require('bluebird');

    var promiseWhile = function(condition, body) {
    var resolver = Promise.defer();

    var loop = function() {
    if (!condition()) return resolver.resolve();
    return Promise.cast(body())
    .then(loop)
    .catch(resolver.reject);
    };

    process.nextTick(loop);

    return resolver.promise;
    };