we can use New Promise function to write asyncronization call to improve the application performance.
here is sample call to use the New Promise feature from ES6/Javascript
loadLanguage: function () {
return new Promise(
function (resolve, reject) {
var PreferredLanguages = [{ Description: "English", Code: "en" },
{ Description: "French", Code: "fr" }];
dataViewModel.preferredLanguages(PreferredLanguages);
if (PreferredLanguages) {
resolve(PreferredLanguages);
}
else{
reject("no Language load");
}
}
);
},
however this feature is currently not support by all browser which it does not work in IE11 and below.
fortunately we still can make it work around with $.Deferred from JQuery
the code below will demonstrate the same result as the previous code within new Promise constructor.
loadLanguage: function () {
var deferred = $.Deferred();
var PreferredLanguages = [{ Description: "English", Code: "en" },
{ Description: "French", Code: "fr" }];
dataViewModel.preferredLanguages(PreferredLanguages);
if (PreferredLanguages) {
deferred.resolve(PreferredLanguages);
}
else {
deferred.reject("no Language load");
}
return deferred.promise();
},
No comments:
Post a Comment