Skip to content

Return the result of the callback in the promise#33

Open
georgehb wants to merge 1 commit intoTheBrainFamily:masterfrom
georgehb:master
Open

Return the result of the callback in the promise#33
georgehb wants to merge 1 commit intoTheBrainFamily:masterfrom
georgehb:master

Conversation

@georgehb
Copy link

This makes the promise returned by wait-for-expect resolve to the same value returned by the supplied callback

This would allow you to do something like this without having to repeat the call to getNumber:

const value = await waitForExpect(async() => {
    const val = await getNumber() // returns some number that might/should be greater than 50
    expect(val).toBeGreaterThan(50)
    return val
})
console.log(value > 50) // true
// Do something with value

My motivation for this is for use in the pptr-testing-library where it's wait function is (more or less) an alias for waitForExpect. In short I often want to wait for an element to exist on the page and then do something with the element. ie what I would like to do is this

const $button = await wait(() => $document.getByRole("button"))
// Do something with $button ...

@sadams
Copy link

sadams commented Sep 13, 2024

@georgehb i know it's been a while but if you are interested i merged your PR on my fork of this project: #39 (comment)
Hope you don't mind!

@devlato
Copy link

devlato commented Jan 29, 2025

I can recommend an alternative if you're interested :)

https://github.com/devlato/async-wait-until

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants