Last active
August 29, 2015 14:18
-
-
Save m1sta/218b793d105a5590bc85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbExample (callback) -> | |
try | |
dataToStore = JSON.stringify(hello:'world') | |
await db.put(id: 123, data: dataToStore) | |
resultString = await db.get(id: 123) | |
resultJSON = try JSON.parse(resultString) catch resultString | |
catch (err) | |
if (err.status !== 409) throw err | |
finally | |
callback(err, resultJSON) | |
closureExample -> | |
funcs = {} | |
for (i in 1 to 3) funcs[i] = (msg, #i) -> msg + i | |
for (key, value in funcs) console.log(*) = key + ": " + value("I am ") | |
list = (#) -> for(i = 1 to 20) if (i % 2 == 0) yield i | |
interpolationExample (input) -> | |
output = "Wrapped #{input}" | |
[first, second] = [1, 2] | |
console.log(a.b.c?) where a = {b: {c:123}} | |
return {input, output, first, second} | |
dbExample (callback) -> | |
try | |
dataToStore = JSON.stringify(hello:'world') | |
await db.put(id: 123, data: dataToStore) | |
resultString = await db.get(id: 123) | |
resultJSON = try JSON.parse(resultString) catch resultString | |
catch (err) | |
if (err.status !== 409) throw err | |
finally | |
callback(err, resultJSON) | |
closureExample -> | |
funcs = {} | |
for (i in 1 to 3) funcs[i] = (msg, #i) -> msg + i | |
for (key, value in funcs) console.log(*) = key + ": " + value("I am ") | |
list = (#) -> for(i = 1 to 20) if (i < 10) return i | |
interpolationExample (input) -> | |
output = "Wrapped #{input}" | |
[first, second] = [1, 2] | |
console.log(a.b.c?) where a = {b:{c:123}} | |
return {input, output, first, second} | |
$("button").on("click", *) => console.log(this) | |
promiseExample (url) ~> | |
req = new XMLHttpRequest() | |
req.open('GET', url) | |
req.onload -> | |
if (req.status == 200) resolve(req.response) | |
else reject(*) = Error(req.statusText) | |
req.onerror -> reject(*) = Error('Network Error') | |
req.send() | |
iteratorExample -> | |
yield i++ | |
chainExample -> | |
setTimeout(*, 1000) -> console.log("First") | |
setTimeout(*, 1000) = () -> console.log("Second") | |
console.log(*) = 10 | |
console.log(*) = await db.get('key') >> JSON.parse(*) >> parseInt(*.value) | |
data = (data or []).concat(*) = 'first', 'second' | |
console.log(result) where result = 10 | |
console.log(result) where | |
result = await db.get('key') >> JSON.parse(*) >> parseInt(*.value) | |
data = (base).concat(list) where | |
base = data or [] | |
list = 'first', 'second' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment