Skip to content

Commit fdb7bc5

Browse files
committed
Adding chapter 12 code
1 parent c1ca7d2 commit fdb7bc5

File tree

11 files changed

+24618
-0
lines changed

11 files changed

+24618
-0
lines changed

Chapter 12/traceur1.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class BaseStructure {
2+
constructor() {
3+
console.log("Structure built");
4+
}
5+
}
6+
7+
class Castle extends BaseStructure {
8+
constructor(name){
9+
this.name = name;
10+
super();
11+
}
12+
Build(){
13+
console.log("Castle built: " + this.name);
14+
}
15+
}

Chapter 12/traceur1.out.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var BaseStructure = function BaseStructure() {
2+
"use strict";
3+
console.log("Structure built");
4+
};
5+
($traceurRuntime.createClass)(BaseStructure, {}, {});
6+
var Castle = function Castle(name) {
7+
"use strict";
8+
this.name = name;
9+
$traceurRuntime.superCall(this, $Castle.prototype, "constructor", []);
10+
};
11+
var $Castle = Castle;
12+
($traceurRuntime.createClass)(Castle, {Build: function() {
13+
"use strict";
14+
console.log("Castle built: " + this.name);
15+
}}, {}, BaseStructure);

Chapter 12/traceur2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function CreateFeast(meat, drink = "wine"){
2+
console.log("The meat is: " + meat);
3+
console.log("The drink is: " + drink);
4+
}
5+
CreateFeast("Boar", "Beer");
6+
CreateFeast("Venison");

Chapter 12/traceur2.out.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function CreateFeast(meat) {
2+
var drink = arguments[1] !== (void 0) ? arguments[1] : "wine";
3+
console.log("The meat is: " + meat);
4+
console.log("The drink is: " + drink);
5+
}
6+
CreateFeast("Boar", "Beer");
7+
CreateFeast("Venison");

Chapter 12/traceur3.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var army1Size = 5000;
2+
var army2Size = 3578;
3+
console.log(`The surviving army will be ${army1Size > army2Size ? "Army 1": "Army 2"}`);

Chapter 12/traceur3.out.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var army1Size = 5000;
2+
var army2Size = 3578;
3+
console.log(("The surviving army will be " + (army1Size > army2Size ? "Army 1" : "Army 2")));

Chapter 12/traceur4.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if(true)
2+
{
3+
var outside = 9;
4+
let inside = 7;
5+
}
6+
7+
console.log(outside);
8+
console.log(inside);

Chapter 12/traceur4.out.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var inside$__0;
2+
if (true) {
3+
var outside = 9;
4+
inside$__0 = 7;
5+
}
6+
console.log(outside);
7+
console.log(inside);

Chapter 12/traceur5.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function timeout(ms) {
2+
return new Promise((resolve) => {
3+
setTimeout(resolve, ms);
4+
});
5+
}
6+
7+
async function asyncValue(value) {
8+
await timeout(1500);
9+
return value;
10+
}
11+
12+
(async function() {
13+
console.log("Starting.");
14+
var valuePromise = asyncValue(42).catch(console.error.bind(console));
15+
console.log("Task is running in the background.");
16+
console.log("Awaiting the promise.");
17+
var value = await valuePromise;
18+
console.log("Promise resolved.");
19+
assert.equal(42, value);
20+
console.log(value);
21+
done();
22+
})();

Chapter 12/traceur5.out.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function timeout(ms) {
2+
return new Promise((function(resolve) {
3+
setTimeout(resolve, ms);
4+
}));
5+
}
6+
function asyncValue(value) {
7+
return $traceurRuntime.asyncWrap(function($ctx) {
8+
while (true)
9+
switch ($ctx.state) {
10+
case 0:
11+
Promise.resolve(timeout(1500)).then($ctx.createCallback(3), $ctx.errback);
12+
return;
13+
case 3:
14+
$ctx.returnValue = value;
15+
$ctx.state = 5;
16+
break;
17+
case 5:
18+
$ctx.state = -2;
19+
break;
20+
default:
21+
return $ctx.end();
22+
}
23+
}, this);
24+
}
25+
(function() {
26+
var valuePromise,
27+
value;
28+
return $traceurRuntime.asyncWrap(function($ctx) {
29+
while (true)
30+
switch ($ctx.state) {
31+
case 0:
32+
console.log("Starting.");
33+
valuePromise = asyncValue(42).catch(console.error.bind(console));
34+
console.log("Task is running in the background.");
35+
console.log("Awaiting the promise.");
36+
$ctx.state = 5;
37+
break;
38+
case 5:
39+
Promise.resolve(valuePromise).then($ctx.createCallback(2), $ctx.errback);
40+
return;
41+
case 2:
42+
value = $ctx.value;
43+
$ctx.state = 3;
44+
break;
45+
case 3:
46+
console.log("Promise resolved.");
47+
assert.equal(42, value);
48+
console.log(value);
49+
done();
50+
$ctx.state = -2;
51+
break;
52+
default:
53+
return $ctx.end();
54+
}
55+
}, this);
56+
})();

0 commit comments

Comments
 (0)