I’m having some trouble with a Promise stored in a member variable, where the 
’then’ block attached to the promise calls code that ends up clearing that 
variable. This then triggers a kj exception that a Promise is being destructed 
while it’s being processed. I understand why this is an error, but I can’t 
figure out how else to clean up that variable.

The situation is like:
        std::optional<Promise<void>> _promise;  // This is a data member of my 
class

        // In a method of my class:
        _promise = something.then([=] {
                …do the work…
                _promise.clear();               // <— boom
        });
How can I make sure _promise is cleared after its handler completes?

I need a specific reference to the promise — I can’t just shove it into a 
TaskSet — because I have code in other methods that clears _promise when it 
decides that it’s no longer needed. (Specifically, this is an idle timer, so 
when an action occurs it needs to clear the timer.)

I can think of a few ways to fix this, but they feel like hacks. I get the 
feeling this is a situation that comes up in various circumstances — a promise 
whose resolution has to clear the Promise variable. Is there a more established 
best practice for handling it?

—Jens

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/8CC19E73-C543-4689-ABA9-5D612D98C026%40mooseyard.com.

Reply via email to