-
Hi there, I'm trying to use this function processFile(left: number, file: { key: string, value: Buffer }) {
let result: Uint8Array | undefined;
bufToU8(file.value, (buf: Uint8Array) => {
// With fflate, we can choose which files we want to compress
zipObj[file.key] = [buf, {
level: 0
}];
// If we didn't want to specify options:
// zipObj[file.name] = buf;
});
if (!--left) {
zip(zipObj, function (err, out) {
if (err) { console.error(err); throw err; }
/* PROBLEM HAPPENS HERE */
result = out
console.log("Done zipping");
/* THIS WORKS, BUT I NEED A WAY TO RETURN FROM THE `processFile` FUNCTION */
// fs.writeFile('test.zip', out)
});
}
return result
} Thanks for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The problem is that Also, I'm not sure the |
Beta Was this translation helpful? Give feedback.
-
Issue may be closed now. |
Beta Was this translation helpful? Give feedback.
The problem is that
zip
is an asynchronous function, but you want to use it in a synchronous function. Asynchronous functions can only be used with callbacks or (preferably)Promise
. In other words, what you're asking is not possible; you should probably create a callback parameter forprocessFile
to "return" your value.Also, I'm not sure the
--left
bit of this code will work if you callprocessFile
multiple times, as it doesn't actually decrement the value in the caller.left
is passed by value toprocessFile
, not as a reference.