Skip to content

Instantly share code, notes, and snippets.

@chinmay185
Last active August 29, 2015 14:19
Show Gist options
  • Save chinmay185/035a07cc0234a75ece03 to your computer and use it in GitHub Desktop.
Save chinmay185/035a07cc0234a75ece03 to your computer and use it in GitHub Desktop.

Revisions

  1. chinmay185 revised this gist Apr 28, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ var request = function(url) {
    };
    var saveResponse = function(response) {
    // save response to file/db (returns a promise)
    }
    };

    apiUrls
    .reduce(function (accumulatedPromise, url) {
  2. chinmay185 revised this gist Apr 28, 2015. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,14 @@ var saveResponse = function(response) {
    // save response to file/db (returns a promise)
    }

    apiUrls.reduce(function(accumulatedPromise, url) {
    return accumulatedPromise
    .then(function() {
    return request(url);
    })
    .then(saveResponse);
    }, Promise.resolve()).then(function(){
    console.log("all done.");
    });
    apiUrls
    .reduce(function (accumulatedPromise, url) {
    return accumulatedPromise
    .then(function () {
    return request(url);
    })
    .then(saveResponse);
    }, Promise.resolve())
    .then(function () {
    console.log("all done.");
    });
  3. chinmay185 revised this gist Apr 28, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,6 @@ apiUrls.reduce(function(accumulatedPromise, url) {
    return request(url);
    })
    .then(saveResponse);
    }, Promise.resolve());
    }, Promise.resolve()).then(function(){
    console.log("all done.");
    });
  4. chinmay185 revised this gist Apr 24, 2015. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@
    var Promise = require("bluebird");
    var usernames = ["tom", "harry", "jack"];
    var apiUrls = ["url1", "url2", "url3"];

    var getUser = function(username) {
    return Promise.delay(1000).then(function() {
    return username + " user";
    });
    var request = function(url) {
    // request the rate limited api (returns a promise)
    };
    var saveResponse = function(response) {
    // save response to file/db (returns a promise)
    }

    usernames.reduce(function(accumulatedPromise, username) {
    apiUrls.reduce(function(accumulatedPromise, url) {
    return accumulatedPromise
    .then(function() {
    return getUser(username);
    return request(url);
    })
    .then(console.log);
    .then(saveResponse);
    }, Promise.resolve());
  5. chinmay185 revised this gist Apr 24, 2015. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,15 @@ var Promise = require("bluebird");
    var usernames = ["tom", "harry", "jack"];

    var getUser = function(username) {
    return Promise.delay(1000).then(function() {
    return username + " user";
    });
    return Promise.delay(1000).then(function() {
    return username + " user";
    });
    };

    usernames.reduce(function(accumulatedPromise, username) {
    return accumulatedPromise.then(function() {
    return getUser(username);
    }).then(console.log);
    return accumulatedPromise
    .then(function() {
    return getUser(username);
    })
    .then(console.log);
    }, Promise.resolve());
  6. chinmay185 created this gist Apr 24, 2015.
    14 changes: 14 additions & 0 deletions sequential-async.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    var Promise = require("bluebird");
    var usernames = ["tom", "harry", "jack"];

    var getUser = function(username) {
    return Promise.delay(1000).then(function() {
    return username + " user";
    });
    };

    usernames.reduce(function(accumulatedPromise, username) {
    return accumulatedPromise.then(function() {
    return getUser(username);
    }).then(console.log);
    }, Promise.resolve());