Moya: Concurrent independent requests

Created on 19 Aug 2018  路  4Comments  路  Source: Moya/Moya

I am using Moya version: 11.0.2.

I want to call multiple APIs which are independent of each other. Can I do that with Moya? Is Moya providing this type of facility like i can call multiple API and it will notify me once all the API response is received.

The APIs are independent of each so the order of the API don't matter for me.

I want similar functionality like Promiss.all... then in the Javascript. Where I add multiple promises and in single then I get final callback once all APIs are completed.

question

Most helpful comment

func request(_ target: Target) -> Promise<Response> {
        return Promise<Response> { seal in
            self.request(target, callbackQueue: .main, progress: nil, completion: { result in
                switch result {
                case let .success(response):
                    seal.fulfill(response)
                case let .failure(error):
                    seal.reject(error)
                }
            })
        }
    }

And now you can use it:

when(fulfilled: userProvider.request(.get), postsProvider.request(.get))

All 4 comments

I'm not sure that Moya provides such a functionality. But you still can use Moya together with PromiseKit to achieve the behaviour described above.

@slavasemeniuk Yes. I was thinking for the same. Do you have any reference of wrapper which shows Moya with PromiseKit.

func request(_ target: Target) -> Promise<Response> {
        return Promise<Response> { seal in
            self.request(target, callbackQueue: .main, progress: nil, completion: { result in
                switch result {
                case let .success(response):
                    seal.fulfill(response)
                case let .failure(error):
                    seal.reject(error)
                }
            })
        }
    }

And now you can use it:

when(fulfilled: userProvider.request(.get), postsProvider.request(.get))

Okay. Thanks for the update. It worked with your code snippet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GurpalRajput picture GurpalRajput  路  3Comments

ghost picture ghost  路  3Comments

Tynox picture Tynox  路  3Comments

kamwysoc picture kamwysoc  路  3Comments

JoeFerrucci picture JoeFerrucci  路  3Comments