Last active
August 29, 2015 14:26
-
-
Save myndzi/a9dd98fd9f663fc41508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var errors = require('errors'); | |
errors.create({ name: 'SaveAndQuit', scope: module.exports }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var bhttp = require('bhttp'); | |
var errors = require('./errors'); | |
var counter = 0, promise = Promise.resolve(); | |
var readline = require('readline'); | |
var rl = Promise.promisifyAll(readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
})); | |
function request(url) { | |
// 'promise' starts out set to a resolved promise, so this goes through | |
// and returns the request as normal. any time we change 'promise', the | |
// request will now block pending the outcome; if we reject, then the | |
// request is not made and the error bubbles up. | |
if (counter++ === 100) { // only prompt on #100 | |
promise = promptUser().tap(function () { | |
counter = 0; // only reset when prompt is complete | |
}); | |
} | |
return promise.then(function () { | |
return bhttp.request(url); | |
}); | |
} | |
function promptUser() { | |
return rl.questionAsync("Continue [Y/n]? ").then(function(answer) { | |
// empty line, 'y', or 'yes'; case insensitive | |
// any of these -> resolve promise, continue as normal | |
if (/^(y(es)?)?$/i.test(answer)) { return; } | |
throw new errors.SaveAndQuit(); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var request = require('./request'); | |
var errors = require('./errors'); | |
function paginate(url) { | |
var allTheData = [ ]; | |
return request(url).then(function (data) { | |
Array.prototype.push.apply(allTheData, data); // concatenate 'data' to 'allTheData' | |
if (data.nextUrl) { return paginate(data.nextUrl); } | |
return allTheData; | |
}).catch(errors.SaveAndQuit, function (err) { | |
err.currentData = allTheData; | |
err.currentUrl = url; | |
throw err; | |
}); | |
} | |
function getAllTheData() { | |
return paginate('http://api.com/foos') | |
.then(function () { | |
// i've got all the data! | |
}) | |
.catch(errors.SaveAndQuit, function (err) { | |
// err.currentUrl is the next request we haven't made yet | |
// err.currentData is the partially complete data | |
// return a promise to write this stuff wherever you want, then | |
// either throw a new error so the code that called us knows that we're not done | |
// or just do nothing if it's okay for that to happen | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment