Skip to content

Best practices regarding AsyncPromise lifetime management in C++ TurboModule #216

Answered by hsjoberg
hsjoberg asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @NathHorrigan. I did, yes. I forgot to get back to this thread so thank you for pinging me.

For the example code I gave in this post, the solution I found was that you can mark the lambda as mutable, and make a copy of the promise like this:

auto promise = facebook::react::AsyncPromise<std::string>(rt, jsInvoker_);

std::thread t([promise]() mutable {
  promise.resolve("test");
});
t.detach();

return promise;

Note: you may want resolve within a jsInvoker_->invokeAsync() to make sure it runs on the main thread.

The mutable keyword makes sure you're allowed to copy it and mutate things within the lambda, and you're able to call resolve on the promise (it will de-allocate itself afterwar…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by hsjoberg
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants