Why do I get 'Suspension functions can be called only within coroutine body' if I don't add apply {}

Viewed 66

Could someone please explain why I get a build error

Suspension functions can be called only within coroutine body

for ::anotherSuspenseFunction in following snippet

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {})
    }
}

I don't get it when I add an empty apply on the end of call.

suspend fun anotherSuspenseFunction() = Response.success(BaseResultDataModel())

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
    submit(scope) {
        responseHandler.call(scope,
            ::anotherSuspenseFunction,
            {
                BaseResultDataModel()
            },
            {}).apply {  }
    }
}

This also works:

suspend fun example(scope: CoroutineScope, journeyDataModel: JourneyDataModel) {
        responseHandler.call(scope,
          ::anotherSuspenseFunction,
          {
              BaseResultDataModel()
          },
          {})
}

Submit signature looks like this:

protected suspend fun <T> submit(scope: CoroutineScope, query: suspend () -> NetworkResource<T>): NetworkResource<T>

Call signature looks like this:

    suspend fun <ResponseType, ParsedType : BaseResultDataModel, ConvertedType> call(scope: CoroutineScope,
    query: suspend () -> Response<ResponseType>,
    extractor: suspend (ResponseType) -> ParsedType,
    converter: suspend (ParsedType) -> ConvertedType): NetworkResource<ConvertedType> 
0 Answers
Related