no, something like: ``` suspend fun Db.saveAsync()...
# announcements
g
no, something like:
Copy code
suspend fun Db.saveAsync() = async(DbPool) { db.save() }
db.saveAsync().await()

or 

suspend fun Db.saveAsync() = run(DbPool) { db.save() }
db.saveAsync()
q
@gildor could u plz help me some more. if i want to handle saving? how i can to do this? for examle im ready to return ansver to client right after reseveving request, and then will be async saving, but after saving is complite i want to do something more with it? Its much hurder to understand for me, then callback invocation (bow)
g
You have two options: 1. Return successful answer (or just Unit) or throw exception. So exactly what you have with synchronous save 2. Use some result object that has two states success (with result) and fail (with exception object). Kotlin sealed classes really good for this So, because coroutines sequential by default you just call:
Copy code
db.saveAsync()
doSomethingElse()
doSomethingElse() will be called after db save will be finished
q
Copy code
doSomthingElse()
should be
Copy code
suspend
func and should be called into async func?
g
up to you
the only difference is that
saveAsync()
doesn’t lock thread from which you called it
and allows you to write code in synchronous style instead callbacks/features
q
have a really strange behavior, then i call action of controller manually - from browser - saving complited normally, but if i run a tests - saveAsync will not be invoke
just modify suspend func like this
Copy code
suspend fun NotificationRepository.sendAndSaveAsync(notification: Notification) = run(CommonPool) {
    with(notification) {
        messageType!!.sender.makeRequest(notification)
        responseBody = messageType!!.sender.getResponse(this)
        save(this)
    }
}!!
and invoke it like this
Copy code
async(CommonPool) { saveAsync(notification) }
g
If you run async() don’t forget to call .join()
Also, just general advice. Don’t use CommonPool for blocking operations. Create a separate pool for that
I don’t understand your problem about “saveAsync will not be invoke”
q
then i run my tests saving is not invoied
Copy code
notificationsForSendRequests.forEach {
                val json = mockMvc.perform(it)
                        .andExpect(status().isOk)
                        .andReturn().response.contentAsString
                val identifier = JsonPath.read<Long>(json, "$.identifier")
                assertTrue(identifier > 0)
but if i run app, and send a rest request - everything is fine
g
I don't understand by this code fragment what's went wrong in this test code
q
what di u mean "dont forget to call join()"?
async(pool) havent a job as i understand