Skip to content

Commit 229bfe9

Browse files
committed
mastering js callbacks
1 parent dbd8b03 commit 229bfe9

8 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎mastering-callbacks/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Mastering JavaScript Callbacks
2+
3+
> [https://www.youtube.com/watch?v=qN0dkXj7jc0](https://www.youtube.com/watch?v=qN0dkXj7jc0)
4+
5+
Install [io.js](https://iojs.org/en/index.html).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `node index.js` to run the node examples and `npm start` to run the browser examples.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
grizzly
2+
polar
3+
brown

‎mastering-callbacks/bears.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
grizzly
2+
polar
3+
brown
4+
koala
5+
cheetah

‎mastering-callbacks/brown.jpg‎

299 KB
Loading

‎mastering-callbacks/grizzly.jpg‎

7.3 KB
Loading

‎mastering-callbacks/index.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Callback Chain
2+
var fs = require('fs')
3+
4+
function getBears (filepath, done) {
5+
fs.readFile(filepath, function (err, bears) {
6+
if (err) return done(err)
7+
8+
fs.readFile('bears.dictionary', function (err, dict) {
9+
if (err) return done(err)
10+
11+
compareBears(bears, dict)
12+
})
13+
})
14+
15+
function compareBears (bears, dict) {
16+
dict = dict.toString().split('\n')
17+
bears = bears.toString().split('\n').filter(function (bear) {
18+
return dict.indexOf(bear) !== -1
19+
})
20+
done(null, bears)
21+
}
22+
}
23+
24+
getBears('bears.txt', function (err, bears) {
25+
console.log(bears)
26+
})
27+
28+
// Callback Chunks
29+
// var bears = ['grizzly', 'polar', 'brown']
30+
// var count = bears.length
31+
//
32+
// bears = bears.map(function (bear) {
33+
// var img = new Image()
34+
// img.onload = function () {
35+
// next()
36+
// }
37+
// img.src = bear + '.jpg'
38+
// return img
39+
// })
40+
//
41+
// function next() {
42+
// count--
43+
// if (count < 1) {
44+
// bears.forEach(function (bear) {
45+
// console.log(bear.width)
46+
// document.body.appendChild(bear)
47+
// })
48+
// }
49+
// }
50+
51+
// Callback Series
52+
// var bears = ['grizzly', 'polar', 'brown']
53+
// var images = []
54+
//
55+
// function loadBears () {
56+
// var bear = bears.shift()
57+
// if (!bear) {
58+
// console.log('All bears are done', images)
59+
// return
60+
// }
61+
// var img = new Image()
62+
// img.onload = function () {
63+
// loadBears()
64+
// }
65+
// img.src = bear + '.jpg'
66+
// images.push(img)
67+
// }
68+
//
69+
// loadBears()

‎mastering-callbacks/package.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "mastering-callbacks",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js --live"
8+
},
9+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"budo": "^4.0.0"
13+
}
14+
}

‎mastering-callbacks/polar.jpg‎

6.34 KB
Loading

0 commit comments

Comments
 (0)