Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promises extras update #758

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ce76e26
Promises extras update
juandopazo May 16, 2013
b15fbe7
Fix typo in extras tests
juandopazo May 23, 2013
be4c05c
Avoid setting timeouts in test.wait()
juandopazo May 23, 2013
2ef4b58
Better wording for extras tests
juandopazo May 23, 2013
8b61d79
Restore `true` in Resolve prototype declaration
juandopazo May 23, 2013
4d2e906
Favor areSame instead of areEqual in tests
juandopazo May 23, 2013
b6487d7
Use Y.Assert.fail() instead of throwing
juandopazo May 23, 2013
fb61e04
Do not throw errors to indicate a test failure
juandopazo May 23, 2013
078d744
Add early return to resolve() when already resolved
juandopazo May 23, 2013
081be17
Remove other unnecessary uses of throw
juandopazo May 23, 2013
58a4ea6
Test promise.done() synchronously by replacing Y.soon
juandopazo May 23, 2013
74dcfbc
Reuse an already resolved promise in combinators
juandopazo May 24, 2013
1b84527
Correctly name the promise-extras submodule
juandopazo May 24, 2013
f8c7f0e
Avoid a race condition when testing the rejection of every()
juandopazo May 24, 2013
7a429dd
Use try...catch when retrieving obj.then in isPromise
juandopazo May 24, 2013
be9421f
Use utility functions to shorten Y.when()
juandopazo May 24, 2013
2c4b025
Override soon() in multiple tests
juandopazo May 24, 2013
6c4ca93
Add test for some() resolving as fast as possible
juandopazo May 24, 2013
c44b5b0
Test rejecting/fulfilling a fulfilled/rejected promise
juandopazo May 24, 2013
8001de2
Remove unnecessary mutual reference
juandopazo Jun 27, 2013
1c8a672
Add isThenable(), only flatten one level for promises
juandopazo Jul 2, 2013
30c3df0
Remove promise.fail()
juandopazo Jul 2, 2013
cec8703
Fix out of date HISTORY file
juandopazo Jul 2, 2013
7fe746c
Remove tests for fail()
juandopazo Jul 2, 2013
6e9c58e
Add tests for isThenable(), fix tests for isPromise()
juandopazo Jul 2, 2013
0d8db81
Use `this` instead of |this| in tests
juandopazo Jul 2, 2013
5abbbfe
Add tests for thenable assimilation
juandopazo Jul 2, 2013
4e66a2d
Remove merge message from HISTORY
juandopazo Jul 2, 2013
0870329
Combinators take multiple arguments
juandopazo Jul 18, 2013
279569e
Fix batch documentation
juandopazo Jul 18, 2013
0731c53
Merge HISTORY.md
juandopazo Jul 18, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/promise/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Promise Change History
@VERSION@
------

* Split the module into promise-core and promise-extras
* then() no longer returns an instance of this.constructor
* Added combinators, factories and extra methods
* [!] Deprecated Y.batch() in favor of Y.Promise.every()
* The first function received as parameter of the Promise init function is now
a reference to resolve() instead of fulfill()
* Changed the value of |this| inside callbacks to undefined to match the
Promises A+ spec

Expand Down
15 changes: 11 additions & 4 deletions src/promise/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
"name": "promise",

"builds": {
"promise": {
"promise-core": {
"jsfiles": [
"promise.js",
"resolver.js",
"when.js",
"batch.js"
"resolver.js"
]
},
"promise-extras": {
"jsfiles": [
"extras/when.js",
"extras/methods.js",
"extras/factories.js",
"extras/combinators.js",
"extras/batch.js"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/promise/docs/basic-example.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ getUser: function (name) {
```
// Fetches a URL, stores a promise in the cache and returns it
function fetch(url) {
var promise = new Y.Promise(function (fulfill, reject) {
var promise = new Y.Promise(function (resolve, reject) {
Y.jsonp(url, function (res) {
var meta = res.meta,
data = res.data;

// Check for a successful response, otherwise reject the
// promise with the message returned by the GitHub API.
if (meta.status >= 200 && meta.status < 300) {
fulfill(data);
resolve(data);
} else {
reject(new Error(data.message));
}
Expand Down
7 changes: 0 additions & 7 deletions src/promise/docs/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
"modules": ["promise", "jsonp"],
"useModules": ["promise", "jsonp"]
},
{
"name": "subclass-example",
"displayName": "Subclassing Y.Promise",
"description": "Extend Y.Promise to create classes that encapsulate standard transaction logic in descriptive method names",
"modules": ["promise", "jsonp"],
"useModules": ["promise", "jsonp"]
},
{
"name" : "plugin-example",
"displayName": "Creating a Node Plugin that chains transitions",
Expand Down
34 changes: 17 additions & 17 deletions src/promise/docs/index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -476,33 +476,33 @@ MyDatabase.prototype.save = function (key, data) {
};
```

<h3>Non-serial Operation Batching</h3>
<h3>Handling multiple non-sequential operations</h3>

<p>
Promise chaining works great to serialize synchronous and asynchronous
operations, but often several asynchronous operations can be performed
simultaneously. This is where `Y.batch()` comes in.
simultaneously. This is where `Y.Promise.every()` comes in.
</p>

<p>
`Y.batch()` takes any number of promises as arguments, and returns a new
promise that will resolve when all the batched promises have resolved. The
`Y.Promise.every()` takes an array of promises, and returns a new promise
that will resolve when all the promises in the array have resolved. The
resolved value will be an array of values from the individual promises, in
the order they were passed to `Y.batch()`.
the order they were passed to `Y.Promise.every()`.
</p>

<p>
If any one of the batched promises should be rejected, the batch promise
is immediately rejected with that reason, so failures can be dealt with
sooner rather than later.
If any one of the promises in the list should be rejected, the returned
promise is immediately rejected with that reason, so failures can be dealt
with sooner rather than later.
</p>

```
Y.batch(
Y.Promise.every([
getUserAccountInfo(userId),
getUserPosts(userId, { page: 1, postsPerPage: 5 }),
getUserRank(userId)
)
])
.then(function (data) {
var account = data[0],
posts = data[1],
Expand Down Expand Up @@ -564,13 +564,13 @@ Y.batch(
<h4>`Y.Parallel`</h4>

<p>
`Y.Parallel` is similar to `Y.batch` in that it provides a mechanism to
execute a callback when several independent asynchronous operations have
completed. However, it doesn't handle errors or guarantee asynchronous
callback execution. It is also transactional, but the batch of operations
is bound to a specific callback, where `Y.batch()` returns a promise that
represents the aggregated values of those operations. The promise can be
used by multiple consumers if necessary.
`Y.Parallel` is similar to `Y.Promise.every()` in that it provides a
mechanism to execute a callback when several independent asynchronous
operations have completed. However, it doesn't handle errors or guarantee
asynchronous callback execution. It is also transactional, but the list of
operations is bound to a specific callback, where `Y.Promise.every()`
returns a promise that represents the aggregated values of those operations.
The promise can be used by multiple consumers if necessary.
</p>

<h3 id="plans">What are the plans for Promises in the library?</h3>
Expand Down
4 changes: 2 additions & 2 deletions src/promise/docs/partials/github-cache.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ var GitHub = (function () {

// Fetches a URL, stores a promise in the cache and returns it
function fetch(url) {
var promise = new Y.Promise(function (fulfill, reject) {
var promise = new Y.Promise(function (resolve, reject) {
Y.jsonp(url, function (res) {
var meta = res.meta,
data = res.data;

// Check for a successful response, otherwise reject the
// promise with the message returned by the GitHub API.
if (meta.status >= 200 && meta.status < 300) {
fulfill(data);
resolve(data);
} else {
reject(new Error(data.message));
}
Expand Down
4 changes: 2 additions & 2 deletions src/promise/docs/partials/node-plugin-plugin.mustache
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function PromisePlugin(config) {
// Create a private NodePromise instance that points to the plugin host
this._promise = new NodePromise(function (fulfill) {
this._promise = new NodePromise(function (resolve) {
// Since this is a Node plugin, config.host will be an instance of Node
fulfill(config.host);
resolve(config.host);
});
}

Expand Down
18 changes: 8 additions & 10 deletions src/promise/docs/partials/node-plugin-transition.mustache
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// This method takes the same "config" parameter as Node's transition method
// but returns a NodePromise instead
NodePromise.prototype.transition = function (config) {
// We call this.then to ensure the promise is fulfilled.
// Since we will be creating a chain of transitions this means we will be
// waiting for the previous transition to end
return this.then(function (node) {
// As noted in the user guide, returning a promise inside the then()
// callback causes the promise returned by then() to be synced with this
// new promise. This is a way to control when the returned promise is
// fulfilled
return new Y.Promise(function (fulfill, reject) {
var promise = this;

// Return a new NodePromise so that chaining calls to transition()
// results in transition executed sequentially
return new NodePromise(function (resolve, reject) {
// Call promise.then to the previous transition ended
promise.then(function (node) {
node.transition(config, function () {
// The transition is done, signal the promise that all is ready
// by fulfilling it with the same node
fulfill(node);
resolve(node);
});
});
});
Expand Down
24 changes: 0 additions & 24 deletions src/promise/docs/partials/subclass-array-operations.mustache

This file was deleted.

5 changes: 0 additions & 5 deletions src/promise/docs/partials/subclass-css.mustache

This file was deleted.

27 changes: 0 additions & 27 deletions src/promise/docs/partials/subclass-js.mustache

This file was deleted.

6 changes: 0 additions & 6 deletions src/promise/docs/partials/subclass-toarray-fn.mustache

This file was deleted.

66 changes: 0 additions & 66 deletions src/promise/docs/subclass-example.mustache

This file was deleted.

15 changes: 8 additions & 7 deletions src/promise/js/batch.js → src/promise/js/extras/batch.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
var slice = [].slice;

/**
Returns a new promise that will be resolved when all operations have completed.
Takes both any numer of values as arguments. If an argument is a not a promise,
it will be wrapped in a new promise, same as in `Y.when()`.
Takes both callbacks and promises as arguments. If an argument is a callback,
it will be wrapped in a new promise.

@for YUI
@method batch
@param {Any} operation* Any number of Y.Promise objects or regular JS values
@return {Promise} Promise to be fulfilled when all provided promises are
resolved
@param {Function|Promise} operation* Any number of functions or Y.Promise
objects
@return {Promise}
@deprecated @SINCE@
**/
Y.batch = function () {
Y.log('batch() was deprecated in YUI @SINCE@. Use Promise.every()', 'warn');

var funcs = slice.call(arguments),
remaining = funcs.length,
i = 0,
Expand Down
Loading