Last active
August 29, 2015 14:02
Question: Promise resolved handling
This file contains hidden or 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 | |
}; |
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
This might be a good question for es-discuss.