Dumb DOM API questions

What’s the best way to free a handle in a chain of promises? I find myself having to “nest” my calls instead of chaining them because if I simply chain them I lose the reference of the handle used previously so I can’t free it once the chain is done.

Or a simple example of the problem:

window.safeMutableDataEntries.forEach(entriesHandle, (k, v) => {

    let key = String.fromCharCode.apply(null, k);
    let value = String.fromCharCode.apply(null, new Uint8Array(v.buf));

    console.log(key + ': ' + value);
  }).then(_ => {
    // You can't free entriesHandle here since it is not in scope
    return 'Iteration complete' + _;
  });
}

What I end up doing is create a variable outside of the promise, assign the handle to it and then free it using the outside variable like:

var handle_to_erase;
window.safeMutableDataEntries.forEach(entriesHandle, (k, v) => {
handle_to_erase = entriesHandle;
let key = String.fromCharCode.apply(null, k);
let value = String.fromCharCode.apply(null, new Uint8Array(v.buf));

    console.log(key + ': ' + value);
  }).then(_ => {
    window.safeMutableDataEntries.free(handle_to_erase);
    return 'Iteration complete' + _;
  });
}

So what’s the best way to handle handles? :slight_smile:

2 Likes