Skip to content

Commit

Permalink
fix: Fix Promise::awaitFuture actually locking threads (#347)
Browse files Browse the repository at this point in the history
* fix: Fix `Promise::awaitFuture` actually locking threads

* fix: Make tests only wait 1s instead of 2s
  • Loading branch information
mrousavy authored Nov 18, 2024
1 parent ae7cab0 commit 8cd7a29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
6 changes: 3 additions & 3 deletions example/src/getTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,10 @@ export function getTests(
(
await it(async () => {
const start = performance.now()
// 2s + 2s = ~4s in serial, ~2s in parallel
await Promise.all([testObject.wait(2), testObject.wait(2)])
// 0.5s + 0.5s = ~1s in serial, ~0.5s in parallel
await Promise.all([testObject.wait(0.5), testObject.wait(0.5)])
const end = performance.now()
const didRunInParallel = end - start < 4000
const didRunInParallel = end - start < 1000
return didRunInParallel
})
)
Expand Down
14 changes: 4 additions & 10 deletions packages/react-native-nitro-modules/cpp/core/Promise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ class Promise final {
* Once the future resolves or rejects, the Promise resolves or rejects.
*/
static std::shared_ptr<Promise> awaitFuture(std::future<TResult>&& future) {
std::shared_future<TResult> sharedFuture(std::move(future));
return async([sharedFuture = std::move(sharedFuture)]() {
sharedFuture.wait();
return sharedFuture.get();
});
auto sharedFuture = std::make_shared<std::future<TResult>>(std::move(future));
return async([sharedFuture = std::move(sharedFuture)]() { return sharedFuture->get(); });
}

/**
Expand Down Expand Up @@ -230,11 +227,8 @@ class Promise<void, TError> final {
}

static std::shared_ptr<Promise> awaitFuture(std::future<void>&& future) {
std::shared_future<void> sharedFuture(std::move(future));
return async([sharedFuture = std::move(sharedFuture)]() {
sharedFuture.wait();
sharedFuture.get();
});
auto sharedFuture = std::make_shared<std::future<void>>(std::move(future));
return async([sharedFuture = std::move(sharedFuture)]() { sharedFuture->get(); });
}

static std::shared_ptr<Promise> resolved() {
Expand Down

0 comments on commit 8cd7a29

Please sign in to comment.