-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
43 lines (36 loc) · 913 Bytes
/
callbacks.js
File metadata and controls
43 lines (36 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// **************************************
// The old-n-busted callback way
function getFile(file) {
fakeAjax(file, function(text) {
fileReceived(file, text);
});
}
function fileReceived(file, text) {
// haven't received this text yet?
if (!responses[file]) {
responses[file] = text;
}
var files = [ 'file1', 'file2', 'file3' ];
// loop through responses in order for rendering
for (var i = 0; i < files.length; i++) {
// response received?
if (files[i] in responses) {
// response needs to be rendered?
if (responses[files[i]] !== true) {
output(responses[files[i]]);
responses[files[i]] = true;
}
} else {
// can't render yet
// not complete!
return false;
}
}
console.log('Complete!');
}
// hold responses in whatever order they come back
var responses = {};
// request all files at once in "parallel"
getFile('file1');
getFile('file2');
getFile('file3');