-
Notifications
You must be signed in to change notification settings - Fork 10
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
Fix: Promises reject on failure #22
Conversation
4ce722f
to
aa16fbe
Compare
Friendly Ping @justinfagnani |
lazy-imports-import.html
Outdated
@@ -8,6 +8,7 @@ | |||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | |||
--> | |||
<script> | |||
'use strict'; |
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.
shouldn't this be placed inside of the following IIFE?
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.
Yes, it should. I have been writing in ES6 and haven't actually used 'use strict'
for a while. Good catch, thanks!
I'm a little unsure of this behavior around failures, and whether it's coherent with regular HTML imports. The normal use of HTML imports is not to listen for load and error events, but to have a scripts tag following imports that runs when the imports are complete, whether they failed or not: <link rel="import" href="foo.html">
<link rel="import" href="bar.html">
<script>
// this runs regardless of the success of importing foo.html or bar.html
// but runs after both either loaded or failed.
</script> I see two arguments in favor of handling errors differently:
<dom-module>
<link rel="lazy-import" href="foo.html" group="lazy">
<link rel="lazy-import" href="bar.html" group="lazy">
</dom-module> this.importLazyGroup('lazy').then(() => {
// this runs regardless of the success of importing foo.html or bar.html
// but runs after both either loaded or failed.
});
Let me think a bit, and let me know if you have any thoughts on those arguments. |
@stramel the option I was thinking of was to resolve with an array of results - the load or error events. Though we have to think about what that means when we don't even call importHref because the import is already loaded. |
@justinfagnani I believe that is how it functions currently. Resolves regardless with a result of imports. If the imports have already been loaded it still returns them as "loaded". The current approach requires "boilerplate" for most implementations that want to gracefully fail the UI. You can see https://github.com/PolymerElements/polymer-starter-kit/pull/998/files#diff-33ee33aeb8ac3d69c66204db965c3330R165 this.importLazyGroup(page).then((results) => {
if (results.failed.length > 0 || results.loaded.length < 1)
this._showPage404();
}); vs this.importLazyGroup(page).catch(() => {
this._showPage404();
}); |
I think that snippet above is likely incorrect. There usually shouldn't ever be a 404 of a lazy import - most of the time it's a URL pointing to a static resource. More likely the client is offline. It's possible that a lazy-import could point to a dynamic, possibly unknown resource, but the linter would complain about it, and that's a case where This is what I mean about Promise behavior not corresponding to the static eager import behavior: <link rel="import" href="foo.html">
<link rel="import" href="bar.html">
<script>
// this runs regardless of the success of importing foo.html or bar.html
// but runs after both either loaded or failed.
</script> |
@justinfagnani I agree there shouldn't be with the exception of offline and incorrect group name. The linter wouldn't catch the case as I proposed in the PSK where Also, I'm not sure there is a linting rule for lazy-imports to check static lazy group imports. WRT const resolvedPageUrl = this.resolveUrl('my-' + page + '.html');
new Promise((resolve, reject) => {
Polymer.importHref(
resolvedPageUrl,
resolve,
reject,
true);
}).then(() => {
console.log('success');
}, () => {
console.error('fail');
}); To me, it kinda feels more like that... I do get what you are saying about matching closer to the spec and I would love to match the spec. It just seems like more of a burden to have to check to see if something failed in the results while in the "success" function of the promise. |
If The grouping feature makes matters a bit more complicated, but I think one could argue that a failing import would mean some sort of error-condition, which should be reflected explicitly in a rejection of the promise. |
@justinfagnani Ping |
Ok, I'm convinced :) |
Regarding the specific implementation, I think we should discuss my latest comment in #29 about not explicitly keeping loaded state, but just storing the pending Promises. |
I agree. Will wait until #36 lands before rebasing and adjusting this PR to work with it. |
@justinfagnani Updated based on the merge of #36 with test updates and added a test to check the edge case of #36 as well. |
lazy-imports-import.html
Outdated
return Promise.resolve(importStatuses); | ||
} | ||
if (links.length < 1) { | ||
return Promise.reject(importStatuses); |
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.
Reject should be called with an error though. The error could carry the statuses I guess.
Can you rebase? |
4dcd63d
to
b5714e7
Compare
@justinfagnani Did you get a chance to circle back to this? |
Ping @justinfagnani |
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.
LGTM! Thanks!
Addresses #20
Changes:
@garlicnation @justinfagnani @rictic PTAL
UPDATE: Rebased onto master