Skip to content

Commit 9ab7e76

Browse files
committed
Initial commit
0 parents  commit 9ab7e76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+7130
-0
lines changed

.eslintrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"parser": "babel-eslint",
3+
"rules": {
4+
"indent": [2, 2],
5+
"quotes": [2, "single"],
6+
"linebreak-style": [2, "unix"],
7+
"no-console": 0,
8+
"no-unused-vars": 0,
9+
"semi": [2, "always"]
10+
},
11+
"env": {
12+
"es6": true,
13+
"node": true,
14+
"browser": true
15+
},
16+
"extends": "eslint:recommended"
17+
}

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history
34+
Gemfile.lock
35+
demo/public/assets/bundle.js

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gem 'guard'
2+
gem 'guard-shell'
3+
gem 'guard-livereload'

Guardfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
guard :shell do
2+
watch %r{.*code/.*\.js$} do |m|
3+
`./copy-syntax.sh #{m[0]}`
4+
end
5+
end
6+
7+
guard 'livereload' do
8+
watch 'demo/public/index.html'
9+
watch %r{demo/public/assets/.*\.css}
10+
end

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jeremy Fairbank
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# rise-of-async-js-talk
2+
Code samples, demos, and resources for "The Rise of Async JavaScript" talk

code/await.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function fetchOrder(orderId) {
2+
return fetch(`/orders/${orderId}`)
3+
.then(response => response.json());
4+
}
5+
6+
async printOrder(orderId) {
7+
const order = await fetchOrder(orderId);
8+
console.log(order);
9+
}
10+
11+
function printOrder(orderId) {
12+
fetchOrder(orderId)
13+
.then(order => console.log(order));
14+
}

code/babel/babelrc-manual.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": [
4+
"syntax-async-functions",
5+
"transform-regenerator",
6+
"transform-runtime"
7+
]
8+
}

code/babel/babelrc-stage-3.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015", "stage-3"],
3+
"plugins": ["transform-runtime"]
4+
}

code/error-handling/catchable.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
promise.then(() => {
2+
throw new Error('Fail');
3+
});
4+
5+
new Promise((resolve, reject) => {
6+
reject(new Error('Fail'));
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function fetchOrder(orderId) {
2+
return fetch(`/orders/${orderId}`)
3+
.then(resp => {
4+
if (resp.status === 404) {
5+
throw new Error('Order not found');
6+
}
7+
return resp.json();
8+
});
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function fetchOrder(orderId) {
2+
return new Promise((resolve, reject) => {
3+
$.get(`/orders/${orderId}`)
4+
.fail(resp => {
5+
if (resp.status === 404) {
6+
reject(new Error('Order not found'));
7+
}
8+
});
9+
});
10+
}

code/error-handling/printOrder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async function printOrder(orderId) {
2+
try {
3+
const order = await fetchOrder(orderId);
4+
console.log(order);
5+
} catch (e) {
6+
console.log('Error retrieving order', e);
7+
}
8+
}

code/flow-control/for-of.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
async function printOrders(orderIds) {
2+
for (const id of orderIds) {
3+
const order = await fetchOrder(id);
4+
console.log(order);
5+
}
6+
}

code/flow-control/if.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
async function printOrder(orderId) {
2+
let order;
3+
4+
if (await orderExists(orderId)) {
5+
order = await fetchOrder(orderId);
6+
} else {
7+
order = await createOrder();
8+
}
9+
10+
return order;
11+
}

code/intro/async.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
async function fetchCustomerNameForOrder(orderId) {
2+
try {
3+
const order = await fetchOrder(orderId);
4+
const customer = await fetchCustomer(order.customerId);
5+
6+
return customer.name;
7+
} catch (err) {
8+
logError(err);
9+
throw err;
10+
}
11+
}

code/intro/nested-callbacks.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function fetchCustomerNameForOrder(orderId, done, fail) {
2+
fetchOrder(orderId, function(err, order) {
3+
if (err) {
4+
logError(err);
5+
fail(err);
6+
} else {
7+
fetchCustomer(
8+
order.customerId,
9+
function(err, customer) {
10+
if (err) {
11+
logError(err);
12+
fail(err);
13+
} else {
14+
done(customer.name);
15+
}
16+
}
17+
);
18+
}
19+
});
20+
}

code/intro/nested-named-callbacks.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function handleCustomer(done, fail) {
2+
return function(err, customer) {
3+
if (err) {
4+
logError(err);
5+
fail(err);
6+
} else {
7+
done(customer.name);
8+
}
9+
};
10+
}
11+
function handleOrder(done, fail) {
12+
return function(err, order) {
13+
if (err) {
14+
logError(err);
15+
fail(err);
16+
} else {
17+
fetchCustomer(
18+
order.customerId,
19+
handleCustomer(done, fail)
20+
);
21+
}
22+
};
23+
}
24+
function fetchCustomerNameForOrder(orderId, done, fail) {
25+
fetchOrder(orderId, handleOrder(done, fail));
26+
}

code/intro/promise-with-catch.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function fetchCustomerNameForOrder(orderId) {
2+
return fetchOrder(orderId)
3+
.then(order => fetchCustomer(order.customerId))
4+
.then(customer => customer.name)
5+
.catch(err => {
6+
logError(err);
7+
throw err;
8+
});
9+
}

code/intro/promise-without-catch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function fetchCustomerNameForOrder(orderId) {
2+
return fetchOrder(orderId)
3+
.then(order => fetchCustomer(order.customerId))
4+
.then(customer => customer.name);
5+
}

code/intro/simple-callback.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fetchUserById(1, function(err, user) {
2+
if (err) {
3+
console.error('Could not retrieve user');
4+
} else {
5+
console.log(user);
6+
}
7+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
async function meaningOfLife() {
2+
return 42;
3+
}
4+
5+
function meaningOfLife() {
6+
return Promise.resolve(42);
7+
}
8+
9+
meaningOfLife()
10+
.then(answer => answer === 42);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
async function meaningOfLife() {
2+
return 42;
3+
}
4+
5+
meaningOfLife() === 42;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
async function printOrders(orderIds) {
2+
const orders = await Promise.all(
3+
orderIds.map(fetchOrder)
4+
);
5+
6+
orders.forEach(order => console.log(order));
7+
}
8+
9+
printOrders([1, 2, 3]);
10+
11+
await Promise.all([
12+
fetchOrder(1),
13+
fetchOrder(2),
14+
fetchOrder(3)
15+
]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
async function printOrders(orderIds) {
2+
for (const id of orderIds) {
3+
const order = await fetchOrder(id);
4+
console.log(order);
5+
}
6+
}
7+
8+
printOrders([1, 2, 3]);
9+
10+
await fetchOrder(1);
11+
await fetchOrder(2);
12+
await fetchOrder(3);

code/transpiledish/originalExample.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function fetchCustomerNameForOrder(orderId) {
2+
return Promise.resolve(fetchOrder(orderId))
3+
.then(order => {
4+
return Promise.resolve(fetchCustomer(order.customerId))
5+
.then(customer => {
6+
return Promise.resolve(customer.name);
7+
});
8+
});
9+
}

code/transpiledish/step1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
async function printOrder(orderId) {
2+
const order = await fetchOrder(orderId);
3+
console.log(order);
4+
}
5+
6+
printOrder(1);

code/transpiledish/step2.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
async function printOrder(orderId) {
2+
const promise = Promise.resolve(fetchOrder(orderId));
3+
4+
console.log(order);
5+
}
6+
7+
printOrder(1);

code/transpiledish/step3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
async function printOrder(orderId) {
2+
const promise = Promise.resolve(fetchOrder(orderId));
3+
4+
return promise.then(order => {
5+
console.log(order);
6+
return Promise.resolve(undefined);
7+
});
8+
}
9+
10+
printOrder(1);

copy-syntax.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
FONT_SIZE=80
4+
FONT_FAMILY=Monaco
5+
STYLE=molokai
6+
SYNTAX=js
7+
OUTPUT=rtf
8+
FILTER=false
9+
START=1
10+
11+
while getopts ":s:S:K:L:f" OPT; do
12+
case $OPT in
13+
s)
14+
STYLE="$OPTARG"
15+
shift 2
16+
;;
17+
S)
18+
SYNTAX="$OPTARG"
19+
shift 2
20+
;;
21+
K)
22+
FONT_SIZE="$OPTARG"
23+
shift 2
24+
;;
25+
L)
26+
START="$OPTARG"
27+
shift 2
28+
;;
29+
f)
30+
FILTER=true
31+
shift
32+
;;
33+
\?)
34+
exit 1
35+
;;
36+
esac
37+
done
38+
39+
FILENAME="$1"
40+
41+
if [[ -z "$FILENAME" ]]; then
42+
>&2 echo 'Please supply filename'
43+
exit 1
44+
fi
45+
46+
if [[ "$FILTER" == true ]]; then
47+
CONTENTS=$(cat "$FILENAME" | grep -Ev "^(?:import|export)")
48+
else
49+
CONTENTS=$(cat "$FILENAME")
50+
fi
51+
52+
echo "$CONTENTS" | tail -n+"$START"| highlight -s "$STYLE" -O "$OUTPUT" -S "$SYNTAX" -K "$FONT_SIZE" -k "$FONT_FAMILY" | tr -d '\n' | pbcopy

0 commit comments

Comments
 (0)