Updates to JS, bluebird, and so on.

This commit is contained in:
Rico Sta. Cruz 2014-09-24 17:09:41 +08:00
parent 0eddf01219
commit 038f8d893a
6 changed files with 220 additions and 4 deletions

92
bluebird.md Normal file
View File

@ -0,0 +1,92 @@
---
title: bluebird.js
layout: default
---
### Creating promises
```js
var Promise = require("bluebird");
new Promise(function (ok, err) {
doStuff(function () {
if (success)
ok();
else
err();
});
})
```
### Consuming promises
```js
promise
.then(okFn, errFn)
.spread(okFn, errFn)
.catch(errFn)
.catch(TypeError, errFn)
.finally(fn)
```
### Handling simultaneous promises
Via Arrays
```js
var promises = [
promise1(), promise2(), ...
]
Promise.all(promises) // succeeds when all succeed
.then(...)
Promise.any(promises) // succeeds if one succeeds first
.then(...)
```
or Objects
```js
Promise.props({
photos: get('photos'),
posts: get('posts')
})
.then(function (res) {
res.photos
res.posts
})
```
### Turn foreign promises into Bluebird promises
```js
Promise.resolve($.get('http://google.com'))
.then(...)
```
### Consuming arrays
```js
getFiles()
.map(function (e) { ... })
.each(function (e) { ... });
```
### Chain of promises
```js
Promise.try(function () {
if (err) throw new Error("boo");
return result;
});
### Working with node-style functions
```js
var readFile = Promise.promisify(fs.readFile);
var fs = Promise.promisifyAll(require('fs'));
```
### References
* https://github.com/petkaantonov/bluebird/blob/master/API.md

37
editorconfig.md Normal file
View File

@ -0,0 +1,37 @@
---
title: editorconfig
layout: default
---
```conf
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
# Tab indentation (no size specified)
[*.js]
indent_style = tab
# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2
# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
```
See: http://EditorConfig.org

38
inline-docs.md Normal file
View File

@ -0,0 +1,38 @@
---
title: Inline documentation
layout: default
---
* Ruby: rdoc
* JavaScript: jsdoc
### RDoc
# Gets a circle's area
#
# @example
#
# area(3)
# #=> 28.27
#
# @param [Number] r The radius of the ricle
# @return [true] If so
#
# == Definition lists
#
# list:: hi.
# +foo+:: parameterized
#
# == Definition lists
# [foo] also
# [bar] like this
http://rdoc.rubyforge.org/RDoc/Markup.html
### Jsdoc
/**
* Ads numbers
*
* @this {Circle}
* @param {Number} r The radius

View File

@ -12,6 +12,7 @@ layout: default
### Subsets
array.slice(0,1) //=> [a]
array.slice(1) //=> [b,c,d,e]
array.slice(1,2) //=> [b]

View File

@ -25,6 +25,8 @@ Enable these options to *not* throw errors in these conditions.
/* jshint expr: true */
production && minify = true;
div.innerWidth;
expect(x).be.true;
/* jshint laxcomma: true */
var one = 1

View File

@ -56,10 +56,22 @@ Multiple args:
### Color operators
#888 + 50% // => #c3c3c3
#888 - 50% // => #444
#888 + 50% // => #c3c3c3 (darken)
#888 - 50% // => #444 (lighten)
#f00 + 50deg // => #ffd500 (hue)
### Casting
n = 5px
foo: (n)em
foo: (n * 5)%
### Lookup
light-blue = #3bd
name = 'blue'
lookup('light-' + name)
### Built-in functions
alpha(#fff) //=> 1
@ -78,8 +90,6 @@ Multiple args:
unquote(string)
s("rgba(0, 0, 0, %s)", 0.3) // Works like eval?
Add Property:
gradient(color)
@ -88,3 +98,39 @@ Add Property:
body
background: gradient(red)
### Conditional
if color == blue
else if true and true
else if 'hey' is not 'bye'
// Aliases:
== is
!= is not
!= isnt
### Definition
if ohnoes is defined
color: blue
### False values
0
null
false
''
### Type check
if val is a 'string'
if val is a 'ident'
### sprintf
'-webkit-gradient(%s, %s, %s)' % (linear (0 0) (0 100%))
// => -webkit-gradient(linear, 0 0, 0 100%)
s("rgba(0, 0, 0, %s)", 0.3)