Fetch with binaries fulfills too early

I need to download some image files by https:

const uxpStorage = require('uxp').storage,
    fs = uxpStorage.localFileSystem; 
const tmpFolder = await fs.getTemporaryFolder();

// download from sourceUrl
const resp = await fetch(sourceUrl); 
const buffer = await resp.arrayBuffer();

// write to local file
const tmpFile = await tmpFolder.createEntry(fileName, {overwrite: true})
await tmpFile.write(buffer, {format: uxpStorage.formats.binary})

This results often in corrupted image files. If I insert a pause after getting the arraybuffer, it mostly works, but that can’t be a proper solution! Like this:

…
const buffer = await resp.arrayBuffer(); 
await timeout(1500);
…

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
} 

I tried to replace the fetch by XMLHttpRequest, using the function from an XD UXP tutorial, but that doesn’t help.

//const resp = await fetch(sourceUrl); 
//const buffer = await resp.arrayBuffer(); 
const buffer =  await xhrBinary(sourceUrl);

Any ideas or suggestions about this?

Have you tried something like this:

const uxpStorage = require('uxp').storage,
    fs = uxpStorage.localFileSystem; 
const tmpFolder = await fs.getTemporaryFolder();
const tmpFile = await tmpFolder.createEntry(fileName, {overwrite: true})

// download from sourceUrl
const resp = await fetch(sourceUrl); 
resp.arrayBuffer().then((buffer)=>{tmpFile.write(buffer, { format: uxpStorage.formats.binary })},(err)=>console.error(err));

Unfortunately, rewriting the arrayBuffer line to Promise syntax didn’t help :smiling_face_with_tear: