135 const file_name = `${file_id}/${(file.name.substring(0,25)).replace(/\.[^/.]+$/, "")}`
136 let extension = file.name.substring(file.name.lastIndexOf("\.")+1)
137 file_names[file_index]=`${file_name}${(extension in image_types && ".webp") || extension}`
138 server.ensureExists(`${__dirname}/user_uploads/${file_id}`,undefined,async (err)=>{139 if(err) {140 console.error(err)141 return;142 }143 if(extension in image_types) {144 writeFile(`${__dirname}/user_uploads/${file_name}.webp`,await sharp(file.data).webp({mixed:true,effort:6}).toBuffer(),(err2)=>{145 if(err2)console.error(err2)146 })147 server.ensureExists(`${__dirname}/user_uploads/previews/${file_id}`,undefined,async (error) => {148 if(error) {149 console.error(error)150 return;151 }152 writeFile(`${__dirname}/user_uploads/previews/${file_name}.webp`,await sharp(file.data).resize(100,100,{fit: "inside"}).webp({mixed:true,effort:6}).toBuffer(),(error2)=>{153 if(error2)console.error(error2)154 })155 })156 } else {157 file.mv(`${__dirname}/user_uploads/${file_name}.${extension}`,(err2)=>{158 if(err2)console.error(err2)159 })160 }161 })162 }
163 }
164 }
499 channelp.classList.add("channel")
500 let textnode = document.createTextNode(channelname)
501 channelp.appendChild(textnode)
502 channelp.addEventListener("click",async function(){503 switchChannel(channelname)504 main()505506 let settings = await (await fetch("/api/settings")).json() // skipqc507 console.log(settings) // skipqc508 if(settings !== "null") {509 if(settings.ACCR === false) {510 unreply()511 }512 }513 })514 tab.appendChild(channelp)
515 }
516}
Writing functions within loops tends to result in errors due to the way the function creates a closure around the loop. For example:
for (var i = 0; i < 10; i++)
funcs[i] = function() {
return i;
};
}
In this case, you would expect each function created within the loop to return a different number. In reality, each function returns 10, because that was the last value of i in the scope.
let
or const
mitigate this problem.
for (let i = 0; i < 10; i++) {
funcs[i] = function() {
return i;
};
}
In this case, each function created within the loop returns a different number as expected.
for (let i = 10; i; i--) {
(function() { return i; })();
}
while(i) {
const a = function() { return i; };
a();
}
do {
function a() { return i; };
a();
} while (i);
let foo = 0;
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
foo += 1;
}
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
}
foo = 100;
let a = function() {};
for (let i = 10; i; i--) {
a();
}
for (let i = 10; i; i--) {
const a = function() {}; // OK, no references to variables in the outer scopes.
a();
}
for (let i = 10; i; i--) {
const a = function() { return i; }; // OK, all references are referring to block scoped variables in the loop.
a();
}
const foo = 100;
for (let i = 10; i; i--) {
const a = function() { return foo; }; // OK, all references are referring to never modified variables.
a();
}
//... no modifications of foo after this loop ...