Add promise and qunit.

This commit is contained in:
Rico Sta. Cruz 2014-09-30 10:04:13 +08:00
parent cbf1fc501e
commit 651c9688d8
3 changed files with 93 additions and 34 deletions

View File

@ -3,43 +3,24 @@ title: bluebird.js
layout: default
---
### Creating promises
```js
var Promise = require("bluebird");
new Promise(function (ok, err) {
doStuff(function () {
if (success)
ok();
else
err();
});
})
```
See [promise](promise.html).
### Consuming promises
```js
promise
.then(okFn, errFn)
.spread(okFn, errFn)
.spread(okFn, errFn) #*
.catch(errFn)
.catch(TypeError, errFn)
.finally(fn)
.catch(TypeError, errFn) #*
.finally(fn) #*
.map(function (e) { ... })
.each(function (e) { ... });
```
### 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(...)
```
@ -57,19 +38,10 @@ Promise.props({
})
```
### 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

59
promise.md Normal file
View File

@ -0,0 +1,59 @@
---
title: Promises
layout: default
---
Based on
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
### Creating promises
```js
new Promise(function (ok, err) {
doStuff(function () {
if (success)
ok();
else
err();
});
})
```
### Consuming promises
```js
promise
.then(okFn, errFn)
.catch(errFn)
```
### Multiple promises
```js
var promises = [
promise1(), promise2(), ...
]
// succeeds when all succeed
Promise.all(promises)
.then(function (values) {
});
// succeeds when one finishes first
Promise.race(promises)
.then(function (value) {
});
```
### Converting other promises
```js
Promise.resolve("reason");
Promise.resolve(promise);
Promise.resolve(thenable);
Promise.reject("reason");
Promise.resolve($.get('http://google.com'))
.then(...)
```

28
qunit.md Normal file
View File

@ -0,0 +1,28 @@
---
title: Qunit
layout: default
---
QUnit.module('a');
QUnit.test('ok', function (t) { ... });
QUnit.moduleStart(function)
QUnit.moduleEnd(function)
### Assertions
t.equal(actual, expected)
t.deepEqual(actual, expected)
t.strictEqual(actual, expected)
t.propEqual(actual, expected)
t.notEqual
t.expect(amount)
### Setup and teardown
QUnit.begin(function (details) {
});
QUnit.done(function (details) {
});