Last active
December 13, 2015 17:28
-
-
Save FGRibreau/4947972 to your computer and use it in GitHub Desktop.
#IUT #Nantes Lectures on NodeJS
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
// Mix async (request) + sync (writeFile) style | |
var request = require('request') | |
, async = require('async') | |
, fs = require('fs') | |
, friends = require('./friends'); | |
function getImgPath(friend){ | |
return './public/'+friend.uid+".jpg"; | |
} | |
/** | |
* Asynchronous Iterator | |
* @param {Object} friend | |
* @param {Function} doneCallback(err, result) | |
*/ | |
function friendIter(friend, doneCallback){ | |
request({ | |
url:friend.pic_big, | |
encoding:'binary' | |
}, function(err, resp, body){ | |
if(err){ | |
doneCallback(err); | |
return; | |
} | |
var file = getImgPath(friend); | |
fs.writeFileSync(file, body, 'binary'); | |
friend.pic_big = file; | |
console.log('Downloaded', file); | |
doneCallback(null, friend); | |
}); | |
} | |
function everythingsDoneCallback(err, updatedFriends){ | |
friends = updatedFriends; | |
console.log('Done !'); | |
} | |
async.map(friends, friendIter, everythingsDoneCallback); |
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
// Full async style | |
var request = require('request') | |
, async = require('async') | |
, fs = require('fs') | |
, friends = require('./friends'); | |
function getImgPath(friend){ | |
return './public/'+friend.uid+".jpg"; | |
} | |
/** | |
* Asynchronous Iterator | |
* @param {Object} friend | |
* @param {Function} doneCallback(err, result) | |
*/ | |
function friendIter(friend, doneCallback){ | |
request({ | |
url:friend.pic_big, | |
encoding:'binary' | |
}, function(err, resp, body){ | |
if(err){doneCallback(err);return;} | |
var filepath = getImgPath(friend); | |
fs.writeFile(filepath, body, 'binary', function(err){ | |
if(err){doneCallback(err);return;} | |
friend.pic_big = filepath; | |
console.log('Downloaded', filepath); | |
doneCallback(null, friend); | |
}); | |
}); | |
} | |
function everythingsDoneCallback(err, updatedFriends){ | |
friends = updatedFriends; | |
console.log('Done !'); | |
} | |
async.map(friends, friendIter, everythingsDoneCallback); |
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
// Full async style with concurrent limit | |
var request = require('request') | |
, async = require('async') | |
, fs = require('fs') | |
, friends = require('./friends'); | |
function getImgPath(friend){ | |
return './public/'+friend.uid+".jpg"; | |
} | |
/** | |
* Asynchronous Iterator | |
* @param {Object} friend | |
* @param {Function} doneCallback(err, result) | |
*/ | |
function friendIter(friend, doneCallback){ | |
request({ | |
url:friend.pic_big, | |
encoding:'binary' | |
}, function(err, resp, body){ | |
if(err){doneCallback(err);return;} | |
var filepath = getImgPath(friend); | |
fs.writeFile(filepath, body, 'binary', function(err){ | |
if(err){doneCallback(err);return;} | |
friend.pic_big = filepath; | |
console.log('Downloaded', filepath); | |
doneCallback(null, friend); | |
}); | |
}); | |
} | |
function everythingsDoneCallback(err, updatedFriends){ | |
friends = updatedFriends; | |
console.log('Done !'); | |
} | |
// https://github.com/caolan/async#maplimitarr-limit-iterator-callback | |
var concurrent = 5; | |
async.mapLimit(friends, concurrent, friendIter, everythingsDoneCallback); |
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
// Full async style with concurrent limit | |
// Read from bottom-up | |
var request = require('request') | |
, async = require('async') | |
, fs = require('fs') | |
, _ = require('lodash') // npm install lodash --save | |
, friends = require('./friends'); | |
function getImgPath(friend){ | |
return './public/'+friend.uid+".jpg"; | |
} | |
function _onImgFileWritten(fn, friend, err){ | |
if(err){fn(err);return;} | |
friend.pic_big = getImgPath(friend); | |
console.log('Downloaded', friend.pic_big); | |
fn(null, friend); | |
} | |
function _onFriendImgData(fn, friend, err, resp, body){ | |
if(err){fn(err);return;} | |
// MDC .bind() (same as partial function application) | |
fs.writeFile(getImgPath(friend), body, 'binary', | |
_onImgFileWritten.bind(null, fn, friend)); | |
} | |
/** | |
* Asynchronous Iterator | |
* @param {Object} friend | |
* @param {Function} doneCallback(err, result) | |
*/ | |
function friendIter(friend, fn){ | |
request({ | |
url:friend.pic_big, | |
encoding:'binary' | |
}, | |
// MDC .bind() (same as partial function application) | |
_onFriendImgData.bind(null, fn, friend)); | |
} | |
function everythingsDoneCallback(err, updatedFriends){ | |
friends = updatedFriends; | |
console.log('Done !'); | |
} | |
async.mapLimit(friends, 5, friendIter, everythingsDoneCallback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment