Is there an integrated way to check if a promise was already resolved? Thoughts on new settleNow() helper #571
Replies: 3 comments
-
Hey @marvinhinz, thanks for bringing this up 👍 From what you're describing, it sounds like you want to check if a promise has settled in order to see if it's still possible to "make it fail." If I understand this correctly, then I think you're looking for the ability to cancel the promise. This is also described in our documentation as follows:
As you can see, when using I hope this answers your question 👍 |
Beta Was this translation helpful? Give feedback.
-
Hey @SimonFrings thanks for your response, thats not quite what i had in mind, but i think it might achieve the same result :) In a nutshell i start an async task that might resolve or fail after timeout in the future, before yielding this promise to wait for it to finish i do other work. That work will most likely complete before the timeout of the async is reached, but at that point i dont really need to wait much longer. I guess if reactphp promises had a simple check function like $ Maybe wrapping it in another timeout |
Beta Was this translation helpful? Give feedback.
-
Please keep in mind that a promise is an intermedia object representing something for communication purposes. Not the action you are performing. If you want to fail/stop/interrupt that action you should do that either through rigging up Have you looked at the deferred object at https://reactphp.org/promise/#deferred-1 ? It grants you a different way of controlling the promise and gives you a direct way to reject it. You can detect if a promise has been already resolve by doing this: $resolved = false;
$promise->then(static function () use (&$resolved): void {
$resolved = true;
});
// $resolved will be true here if the promise has already been resolved |
Beta Was this translation helpful? Give feedback.
-
In my use case im inside a coroutine doing various async calls to control a vending machine during product vending (turning spirals, moving the lift, checking optical barrier etc.) and i start a promise that watches the result of the optical barrier check first (it will resolve only if the barrier was triggered, it retries api calls until the return value changes).
Now after moving the lift i yield the opticalPromise (wrapped in a timeout). But what if i wanted to enforce the promise to fail right now, if its not resolved already. I couldnt find an integrated way to check if a promise was already resolved. If that was possible, an async helper function like "settleNow($promise)" could be very handy. Maybe im missing an obvious solution?
Beta Was this translation helpful? Give feedback.
All reactions