I know this question has been asked many times but strangely the answers in other posts did not solve my problem:
handleFile(files) {
var f = files[0]
var dataSet = JSZip.loadAsync(f).then(function (zip) {
for (var filename in zip.files) {
const one_dicom_file = zip.files[filename]
console.log('--------- one_dicom_file ----------', one_dicom_file)
try {
one_dicom_file.async("arraybuffer").then(function (ArrayBuffer) {
var byteArray = new Uint8Array(ArrayBuffer);
var dataSet = dicomParser.parseDicom(byteArray);
console.log(filename, 'parsed dataSet: ', dataSet);
return dataSet
// EDIT: OP is Missing close curly bracket here!!
} catch (error) {
console.error(error);
}
console.log('check 1')
}
}, function (e) {
console.log(e)
});
}
I'm iterating through the contents of a zip file, and when the first dicom file (a format for medical image) is encountered, I analyze it with a library call dicomParser.parseDicom().
Upon successful execution of this function, I want to quit the iteration immediately. The other dicom files can be ignored for performance purpose.
The above code will, however, print console.log(filename, 'parsed dataSet: ', dataSet); for every single dicom file. The line return dataSet does nothing at all to the for loop. Also, check 1 is printed for every dicom file.
I also tried replacing return dataSet with break, in which case an error Unsyntactic break will be thrown.
Other answers have claimed that both return and break should stop a for loop, but they don't.
I'm very new to javascript so sorry if this is silly question. I'm used to python, and javascript syntax is so over-complicated...
tryis outside the.then(function(ArrayBuffer){callback, and thecatchis inside it - please post code that is syntactically correctasync/awaitinstead of.then()to be able to break out of the loop (and to do the iteration sequentially in the first place)