-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
v2: Backbone.Promise #3651
Open
jridgewell
wants to merge
3
commits into
jashkenas:master
Choose a base branch
from
jridgewell:backbone-promise
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
v2: Backbone.Promise #3651
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
(function() { | ||
|
||
module("Backbone.Promise"); | ||
|
||
test("throws an error if invoked", 1, function() { | ||
try { | ||
Backbone.Promise(); | ||
} catch (e) { | ||
ok(e); | ||
} | ||
}); | ||
|
||
asyncTest(".resolve to passed in value", 1, function() { | ||
var value = {}; | ||
Backbone.Promise.resolve({}).then(function(val) { | ||
strictEqual(val, value); | ||
start(); | ||
}); | ||
}); | ||
|
||
asyncTest(".resolve adopts promise state", 1, function() { | ||
var value = {}; | ||
var promise = Backbone.Promise.resolve(val); | ||
Backbone.Promise.resolve(promise).then(function(val) { | ||
strictEqual(val, value); | ||
start(); | ||
}); | ||
}); | ||
|
||
asyncTest(".resolve executes then callback asynchronously", 1, function() { | ||
var async = false; | ||
Backbone.Promise.resolve().then(function() { | ||
ok(async); | ||
start(); | ||
}); | ||
async = true; | ||
}); | ||
|
||
asyncTest(".reject to passed in value", 1, function() { | ||
var value = {}; | ||
Backbone.Promise.reject({}).then(null, function(val) { | ||
strictEqual(val, value); | ||
start(); | ||
}); | ||
}); | ||
|
||
asyncTest(".reject executes then callback asynchronously", 1, function() { | ||
var async = false; | ||
Backbone.Promise.reject().then(null, function() { | ||
ok(async); | ||
start(); | ||
}); | ||
async = true; | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say a promise implementation should be included by default (I would suggest
npo
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I disagree (feel free to convince me). This seems very similar to polyfilling
JSON
, which we do not provide a default implementation of.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference is BB doesn't use JSON directly. Though I'd be fine with just assumingPromise
is defined globallyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also a little wishy-washy about depending on jQuery's Deferreds. They're badly non-standard, and we're now demanding a dependency on jQuery (or some other promise library) for Models and Collections to work.
We could go our "jQuery require" route, attempting to require some library
'promise'
, then feed that into the factory function. Or just require overwritingBB.Promise
on node if you want to use async methods. Hmmm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As of ES6, we can just expect
Promise
it to be a builtin global (and it is already in many environments) so it'd be exactly as Brad describes, the same as polyfilling JSON. The only requirement here would be that BB wouldn't depend on any specific non-standard library specific methods/helpers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a
if (typeof Promise != "undefined") Backbone.Promise = Promise
line right after our ponyfill using deferreds? You should probably be
pollyfilling Promise anyways if you need it.
On Sun, May 31, 2015, 21:23 Tim Griesser [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do a fallback system:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 for jquery fallback, the implementation differs to greatly from the
promise spec
On Jun 1, 2015 11:06 AM, "Justin Ridgewell" [email protected]
wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@megawac but does it fail for our use case here? Backbone and Underscore tend to opt for the ponyfill approach over pollyfills, encapsulating the desired behavior in functions instead of shimming support globally.
If we force a Promise library (or a Promise global), it'll be confusing to developers why this fails in older browsers but works in newer ones. On the other hand if we set the precedent here that we're ok with making users pollyfill older behaviors, we can start to follow the lead of React in requiring developers to pollyfill native methods (map, forEach, etc).