forked from joaool/MotherGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_QPromises.html
90 lines (84 loc) · 2.96 KB
/
test_QPromises.html
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<title>Q Promises v1.0</title>
<!--<script type="text/javascript" charset="utf-8" src="../rivets/dist/rivets.js" ></script>-->
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="../q-1/q.js" ></script>
</head>
<script>
// Uses jQuery and Q.
var API_KEY = "whatever";
// https://api.sendgrid.com/api/newsletter/lists/email/get.json
// POST Data list=Test&api_user=your_sendgrid_username&api_key=your_sendgrid_password
// function getFlickrResponseAsync(method, responseProperty, params) {
$( document ).ready(function() {
console.log("Begin...");
callDir = function(serverRequest){//ex dir
var deferred = Q.defer();
request=$.ajax({//call to get column definitions frist, then data
url: "http://localhost:3000/"+serverRequest, //to force an error make: url: "http://localhost:3000/xdir",
dataType: 'text'//data type we are expecting from the server
}).done(function(data){
console.log("OKKKKKKKKKKKKKKKK ");
deferred.resolve(data);
}).fail(function(){
console.log("FAILLLLLLLLLLLLLLL");
var error = new Error("Ajax request to localhost:3000/" + serverRequest + " FAILED");
deferred.reject(error);
});
return deferred.promise;
};
// var promise = callDir();
console.log("Now we call callDir()");
callDir("dir").then( //testing a simple promise - use xdir argument to force it to fail !
function(data){
console.log("This is the data for Dir1:\n" + data);
var dirContent = $("<span>" + data + "</span>");
dirContent.insertAfter(".content1");
},
function(error){
console.log(error.message);
var dirContent = $("<span>" + error.message + "</span>");
dirContent.insertAfter(".content1");
}
);
// callDir("dir").then( //testing chained promises
// function(data){
// console.log("This is the data for Dir1:\n" + data);
// var dirContent = $("<span>" + data + "</span>");
// dirContent.insertAfter(".content1");
// callDir("xdir").then(
// function(data){
// console.log("This is the data for Dir2:\n" + data);
// var dirContent = $("<span>" + data + "</span>");
// dirContent.insertAfter(".content2");
// },
// function(error){
// console.log(error.message);
// var dirContent = $("<span>" + error.message + "</span>");
// dirContent.insertAfter(".content2");
// }
// );
// },
// function(error){
// console.log(error.message);
// var dirContent = $("<span>" + error.message + "</span>");
// dirContent.insertAfter(".content1");
// }
// );
console.log("Request was done, before displaying this line !!!");
});
console.log(document.title+"...... END..");
</script>
<body>
<div class="demo First Dir">
<div>Show Dir1</div>
<p class="content1" ><b>Dir 1</b> (on next lines):</p>
</div>
<div class="demo Second Dir">
<div>Show Dir2</div>
<p class="content2" ><b>Dir 2</b> (on next lines):</p>
</div>
</body>
</html>