Permalink
Browse files

some convenience...

  • Loading branch information...
1 parent e5780e5 commit d1fe86f70243713602d0fe3607a68c01970927cd @nilclass committed Jan 18, 2013
Showing with 28 additions and 2 deletions.
  1. +12 −0 README.md
  2. +16 −2 src/promising.js
View
@@ -36,5 +36,17 @@ Rejecting a promise:
return promise;
}
+Most conveniently:
+
+ function myAsyncFunction() {
+ return promising(function(promise) {
+ promise.fulfill();
+ // or
+ promise.reject('foo');
+ // or
+ throw 'foo'; // <<< equals rejecting
+ });
+ });
+
That's about it.
View
@@ -1,4 +1,16 @@
-function getPromise() {
+function getPromise(builder) {
+ var promise;
+
+ if(typeof(builder) === 'function') {
+ setTimeout(function() {
+ try {
+ builder(promise);
+ } catch(e) {
+ promise.reject(e);
+ }
+ }, 0);
+ }
+
var consumers = [], success, result;
function notifyConsumer(consumer) {
@@ -58,7 +70,7 @@ function getPromise() {
}, 0);
}
- return {
+ promise = {
then: function(fulfilled, rejected) {
var consumer = {
@@ -87,6 +99,8 @@ function getPromise() {
}
};
+
+ return promise;
};
module.exports = getPromise;

0 comments on commit d1fe86f

Please sign in to comment.