I have a react function bundled in webpack that resizes image files. I am calling default exported function for resizing. I get the resized image file inside then method of promise which is resolved successfully. But after I assign the value I cant use it anymore it gets undefined after. Here is my code;
var filetoupload;
imageresizer(item.file).then((value) => {
filetoupload = value;
});
var anotherinstance=filetoupload;
item.upload.formData.append('files[]',filetoupload);
Here filetoupload variable is correct in browser debug if I just stop after the code execution but gets undefined in normal run. Why is that? My react code is below:
import ReactDOM from 'react-dom/client';
import React from "react";
import Resizer from "react-image-file-resizer";
const resizeFile = (file) =>
new Promise((resolve) => {
Resizer.imageFileResizer(
file,
80,
80,
"JPEG",
80,
0,
(uri) => {
resolve(uri);
},
"file"
);
});
export default function resize(file) {
return resizeFile(file);
}