Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all() doesn't work on more than 4 items. #109

Closed
nielsz opened this issue Apr 24, 2019 · 6 comments
Closed

all() doesn't work on more than 4 items. #109

nielsz opened this issue Apr 24, 2019 · 6 comments

Comments

@nielsz
Copy link

nielsz commented Apr 24, 2019

class MyA {}
class MyB {}

func getA() -> Promise<MyA> {
    return Promise<MyA> { fullfill, reject in
        print("promise MyA")
        fullfill(MyA())
    }
}

func getB() -> Promise<MyB> {
    return Promise<MyB> { fullfill, reject in
        print("promise MyB")
        fullfill(MyB())
    }
}
        all(
            getA(),
            getA(),
            getA(),
            getB()
        )
        .then { result in
            print("Got result!", result)
                
        }

This method works perfectly.
But If I add one more item to the all() method, I can't compile anymore, with an error on the first item:

Cannot convert value of type 'Promise' to expected argument type 'Promise<_>'

Screenshot 2019-04-24 at 18 55 15

@ghost ghost changed the title any() doesn't work on more than 4 items. all() doesn't work on more than 4 items. Apr 24, 2019
@ghost
Copy link

ghost commented Apr 24, 2019

Hi @nielsz, currently, the max number of dissimilar value types that can be passed to all is 4:
https://github.com/google/promises/blob/master/Sources/Promises/Promise%2BAll.swift#L148

Do you have a specific need to support 5 or more?

@nielsz
Copy link
Author

nielsz commented Apr 24, 2019

Ah that's too bad. I couldn't find that limit in the documentation.
I need to call 6 web-services, preferably at the same time.

@shoumikhin
Copy link
Contributor

shoumikhin commented Apr 24, 2019

Hi Niels,

To make it clear, all() doesn't initiate promises execution in any way, so you can't expect the promises you pass into all() start doing their work simultaneously at that moment. Moreover, you can even pass an already resolved promise to all(), if needed. all() just allows you to wait for a bunch of different promises and get notified when all of them are fulfilled, whenever that happens.

So, you can effectively leverage a combination of all() with nearly the same outcome as if all() really supported 5 or more heterogeneous arguments. For example:

all(all(p1, p2, p3), all(p4, p5, p6)).then { results in
  let ((a1, a2, a3), (a4, a5, a6)) = results
  print(a1, a2, a3, a4, a5, a6)
}

Another option would be extending Promise+All.swift (and Promises+AllTests.swift, of course) to allow a few more arguments. That is somewhat tedious and boilerplate, but feel free to send a PR our way, if that's what you'd prefer.

Thanks.

@ghost
Copy link

ghost commented Apr 30, 2019

@nielsz closing this issue, but feel free to submit a PR if you need the additional functionality for all. Thank you!

@fitsyu
Copy link
Contributor

fitsyu commented Jun 25, 2019

Ah that's too bad. I couldn't find that limit in the documentation.
I need to call 6 web-services, preferably at the same time.

It is nice to have the limit explicitly stated there. :)

@shoumikhin
Copy link
Contributor

@fitsyu we're happy to accept a PR with the proposed change :)

fitsyu added a commit to fitsyu/promises that referenced this issue Jun 25, 2019
ghost pushed a commit that referenced this issue Jul 29, 2019
* add statement about the number of promises `all` can handle

This is extra useful solution for issue #109.

* elaborate notes for limitation of `all` and `any`

* add limitation information for `any` operator

similar to `all` limitation, this one also needs to be explained for users

* fix misplaced words
This issue was closed.
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

No branches or pull requests

3 participants