-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Migrate all jQuery promises to native ES6 Promise
(ref: $.Deferred()
, $.when()
, $.get()
and $.post()
)
#308
Conversation
Promise
(ref: $.Deferred()
, $.get()
and $.post()
functions)Promise
(ref: $.Deferred()
, $.when()
, $.get()
and $.post()
)
this.addFeatures(features); | ||
resolve(features); | ||
}) | ||
.catch(err => reject(err)); |
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.
double check, isn't this a superfluous statement?
.catch(err => reject(err));
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.
.catch(err => reject(err));
I didn't mark them all, but I found this kind of repetition quite often in the code
@@ -11,9 +11,7 @@ inherit(KMLDataProvider, DataProvider); | |||
const proto = KMLDataProvider.prototype; | |||
|
|||
proto.getData = function() { | |||
const d = $.Deferred(); | |||
return d.promise(); | |||
return new Promise((resolve, reject) => {}) |
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.
double check, how can such a promise object be fulfilled?
return new Promise((resolve, reject) => {})
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.
@Raruto i think that KMLDataProvider is never used. We need to double check and remove it
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.
ok, i didn't check in too much detail, but still from some other substitutions you've done i guess your initial intent was to write something like this instead:
return Promise.resolve()
@@ -455,7 +457,7 @@ proto._loadVectorData = function(vectorLayer, bbox) { | |||
vectorLayer.setData(vectorDataResponse.vector.data); | |||
if (this._) return vectorDataResponse; | |||
}) | |||
.fail(() => { | |||
.catch(() => { | |||
return false; | |||
}) |
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.
double check, the following statement can be quite ambiguous:
.catch(() => { return false; })
it is best to make explicit what the intent is (ie. reject
or resolve
the promise) by using one of the following statements:
.catch(() => resolve(false))
.catch(() => reject(false))
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.
@Raruto you can replace .catch(() => { return false; }) with .catch(() => reject(false)). We double check if this method is still used.
url, | ||
data:params | ||
}).then(response => resolve(response)) | ||
.catch(err => reject(err)); |
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.
double check, written that way wouldn't the whole surrounding promise above be superfluous?
return // new Promise((resolve, reject) => {
XHR.post({ ... })
// .then(response => resolve(response))
// .catch(err => reject(err));
// });
.finally(()=> clearTimeout(timeoutKey)); | ||
return d.promise(); | ||
}) | ||
} else this[METHOD]({url, layers, params}) |
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.
😩 Damn weird!
else statement + missing brackets + a multiline istruction
src/app/core/workflow/flow.js
Outdated
// return a promise that will be reolved if all step go right | ||
return d.promise(); | ||
return new Promise((resolve, reject) => { | ||
//** Assign d module variable to an object having resolve, reject methods used on other module method |
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.
Specific code comments (jsdoc, but not only those) on the usefulness of a variable must be written when declaring it otherwise your development IDE is not able to recognize them, that is:
/**
* Module variable having resolve, reject methods used on other module method
*/
let d;
...
d = {
resolve,
reject
};
src/app/main.js
Outdated
@@ -1164,7 +1164,7 @@ ApplicationTemplate.Services = { | |||
floatbar: null | |||
}; | |||
|
|||
ApplicationTemplate.fail = function({language='en', error }) { | |||
ApplicationTemplate.catch = function({language='en', error }) { |
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.
TODO: also double check that it hasn't been renamed elsewhere
ApplicationTemplate.catch = function({language='en', error }) { | |
ApplicationTemplate.fail = function({language='en', error }) { |
src/app/main.js
Outdated
@@ -1205,7 +1205,7 @@ ApplicationService.init() | |||
if (error.responseJSON && error.responseJSON.error.data) error = error.responseJSON.error.data; | |||
else if (error.statusText) error = error.statusText; | |||
} | |||
ApplicationTemplate.fail({ | |||
ApplicationTemplate.catch({ |
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.
ApplicationTemplate.catch({ | |
ApplicationTemplate.fail({ |
@@ -135,7 +135,7 @@ function QueryService(){ | |||
request.then(response => { | |||
const results = this.handleResponse(response, query); | |||
resolve(results); | |||
}).fail(reject) | |||
}).catch(reject) |
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.
double check, written like that the external promise seems superfluous:
return // new Promise((resolve, reject) => {
request.then(response => {
const results = this.handleResponse(response, query);
resolve(results);
})
// .catch(reject)
src/services/data-search.js
Outdated
@@ -44,7 +44,7 @@ function SearchService(){ | |||
resolve({ | |||
data | |||
}) | |||
}).fail(reject) | |||
}).catch(reject) |
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.
return new Promise((resolve, reject) =>{ | ||
this.provider.getFeatures(options) | ||
.then(features => { | ||
this.addFeatures(features); | ||
resolve(features); | ||
}) | ||
.catch(err => reject(err)); |
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.
Not sure what the previous behavior was, but now I think you need to add the return (resolve, reject) => this.provider.getFeatures
from inside the promise to the outside to make . catch(err => reject(err))
also "catchable" from outside of this function (otherwise it will only throw an unhandled fatal error):
return new Promise((resolve, reject) =>{ | |
this.provider.getFeatures(options) | |
.then(features => { | |
this.addFeatures(features); | |
resolve(features); | |
}) | |
.catch(err => reject(err)); | |
return new Promise((resolve, reject) => | |
this.provider | |
.getFeatures(options) | |
.then(features => resolve(this.addFeatures(features))) | |
.catch(err => reject(err)) | |
); |
Anyway i'm not so sure about this statement, check it carefully too
Superseded by: #647 |
Depends on: #452
List of changes
$.Deferred::done()
withPromise::then()
$.Deferred::fail()
withPromise::catch()
$.Deferred::alwas()
withPromise::finally()
$.when()
withPromise::all()
$.get()
with an internal utility function (ref:src/core/utils/utils/XHR::get()
)$.post()
with an internal utility function (ref:src/core/utils/utils/XHR::post()
)addadd new file"util-deprecate"
dependency to effectively report deprecation of some features to final developer (ref:src/deprecate.js
)src/deprecate.js
to effectively report deprecation of some features to final developerg3wsdk.core.utils.resolve(value)
functiong3wsdk.core.utils.reject(value)
functionCloses: #82
Somehow related to: #98