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

A problem proposal: fullfilling rejections #60

Open
ShadowManu opened this issue Nov 2, 2015 · 6 comments
Open

A problem proposal: fullfilling rejections #60

ShadowManu opened this issue Nov 2, 2015 · 6 comments

Comments

@ShadowManu
Copy link
Contributor

Hi. I was just working with mongodb and I sidetracked to thinking on promises and understanding a little more of them. After having solved this workshop (which was great), I was tinkering with a concept on the Promises/A+ spec that we can add as a new exercise. Consider the following code snippet.

var Q = require('q');

var deferred = Q.defer();
var promise = deferred.promise;

var promise2 = promise.then(undefined, function savior(rejection) {
  console.log("Im the savior, who received: " + rejection);
  return {
    then: function(resolve, reject) {
      resolve("A saved rejection");
    }
  };
});

promise2.then(function(value) {
  console.log("The promise 2 was fullfilled with: " + value);
}, function(reason) {
  console.log("The promise 2 was rejected with: " + reason);
});

deferred.reject("A rejection reason");

The output of this program would be

Im the savior, who received: A rejection reason
The promise 2 was fullfilled with: A saved rejection

So, I think the magic to show on this program is in receiving a rejected promise (the promise promise) and then making it's then method return a fullfilled promise (the promise2 promise) with the usage of returning thenables. Somehow the curiosity got me high this night to think on this kind of things (hey, a rime).

A problem on exercising this concept should be a nice addition.

@ShadowManu
Copy link
Contributor Author

Just experimenting and forgot a simpler way: changing the return line with just return 'A saved rejection'; works too.

PD: just using return; works too, but then the promise is fulfilled with undefined.

@TimothyGu
Copy link
Collaborator

Hmm I haven't thought about that. I guess this is a pretty good idea, even though your implementation is a bit odd looking :)

@ShadowManu
Copy link
Contributor Author

That's the comment for. So simplifying a little:

var Q = require('q');

var deferred = Q.defer();
var promise = deferred.promise;

var promise2 = promise.catch(function savior(rejection) {
  console.log("Im the savior, who received: " + rejection);
  return "A saved rejection";
});

promise2.then(function(value) {
  console.log("The promise 2 was fullfilled with: " + value);
}, function(reason) {
  console.log("The promise 2 was rejected with: " + reason);
});

deferred.reject("A rejection reason");

@TimothyGu
Copy link
Collaborator

Yep. There have been a lot of changes done to promise-it-wont-hurt recently: #46 (comment). I will take this as one of my TODOs.

On a side note, it would really help us if you could find time to translate (or to correct) a few strings into Spanish: https://github.com/stevekane/promise-it-wont-hurt/blob/master/i18n/es.json I'm still learning Spanish and unfortunately my accent and vocabulary is distinctively estadounidense :)

@ShadowManu
Copy link
Contributor Author

I could do something about it the wednesday or the weekend to feel helpful ;)

@ShadowManu
Copy link
Contributor Author

Oh what the heck, I peeked at it, seemed easy, and I just did it on #62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants