Last active
August 29, 2015 14:02
-
-
Save rodneyrehm/cccc240e54bdf3304fc5 to your computer and use it in GitHub Desktop.
Question: Promise resolved handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do you prefer alwaysWrapped() or wrappedWhenNeeded() for | |
// a method that depends on some promise being resolved? | |
function FooBar() { | |
this.ready = $.getJSON('something.funky'); | |
} | |
FooBar.prototype.alwaysWrapped = function() { | |
return this.ready.then(function(){ | |
// do something that returns a promise | |
}); | |
}; | |
FooBar.prototype.wrappedWhenNeeded = function() { | |
if (!this.ready.isResolved()) { | |
return this.ready.then(this.wrappedWhenNeeded.bind(this)); | |
} | |
// do something that returns a promise | |
}; |
This might be a good question for es-discuss.
Maybe this:
FooBar.prototype.doStuff = function () {
// just assume .ready is resolved
// do something that returns a promise
};
// then…
var foobar = new FooBar();
foobar.ready.then(foobar.doStuff.bind(foobar));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't know how the second example works, but the first one looks good :)