I want to transform this method, to fully apply to ESLint rules in SAP WEB IDE:
getBase64Promise: function (file) {
return new Promise((resolve, reject) => { // <--
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => { // <--
let encoded = reader.result.replace("data:", "").replace(/^.*;base64,/, ""); // <--
if ((encoded.length % 4) > 0) {
encoded += '='.repeat(4 - (encoded.length % 4));
}
resolve(encoded);
};
reader.onerror = error => reject(error); // <--
});
},
ESLint fails with these rules:
Could anyone help? I tried some things but it does not work at all:
getBase64Promise: function (file) {
return new Promise(function (resolve, reject) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload(function () {
var encoded = reader.result.replace("data:", "").replace(/^.*;base64,/, "");
if ((encoded.length % 4) > 0) {
encoded += "=".repeat(4 - (encoded.length % 4));
}
resolve(encoded);
});
reader.onerror(function () {
reject(reader.error);
});
});
},
Thanks Chris
The =>
or arrow function is a short cut to create an anonymous function. It's also a way of binding the current scope to a function without having to explicitly add .bind(this)
.
You can replace it fairly easily by changing this:
(params) => {
to
function(params) {
So something like
getBase64Promise: function(file) {
return new Promise(function(resolve, reject) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload(function() {
var encoded = reader.result.replace("data:", "").replace(/^.*;base64,/, "");
if ((encoded.length % 4) > 0) {
encoded += "=".repeat(4 - (encoded.length % 4));
}
resolve(encoded);
});
reader.onerror(function() {
reject(reader.error);
});
});