{"id":331660,"date":"2022-04-07T21:00:16","date_gmt":"2022-04-07T21:00:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=331660"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=331660","title":{"rendered":"<span>Kotlin, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 suspend \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u00a0<em>suspend<\/em>\u00a0\u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c?<\/p>\n<p>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432 Kotlin \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u043e\u043c\u00a0<strong><em>suspend<\/em><\/strong>. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438? \u041a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u00a0<em>suspend<\/em>\u00a0\u0431\u043b\u043e\u043a\u0438 \u0432 \u043a\u043e\u0434, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b?<\/p>\n<p>\u0417\u043d\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0432\u0441\u044f \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0438 \u043a\u0430\u043a \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432.<\/p>\n<blockquote>\n<p><em>TL;DR; \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u044d\u0442\u0430 \u043c\u0430\u0448\u0438\u043d\u0430 \u0431\u0435\u0440\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u043e\u0439 \u043d\u0430 \u0441\u0435\u0431\u044f!<\/em><\/p>\n<\/blockquote>\n<p>\u041d\u043e\u0432\u0435\u043d\u044c\u043a\u0438\u0439 \u0432 Android? \u0412\u0437\u0433\u043b\u044f\u043d\u0438 \u043d\u0430 \u044d\u0442\u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043f\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/codelabs.developers.google.com\/codelabs\/kotlin-coroutines\/#0\" rel=\"noopener noreferrer nofollow\">Using coroutines in your Android app<\/a>.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.android.com\/codelabs\/advanced-kotlin-coroutines#0\" rel=\"noopener noreferrer nofollow\">Advanced Coroutines with Kotlin Flow and Live Data<\/a>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0438\u0434\u0435\u043e:<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/624f0c67e1af860b1c6b99f1\" data-style=\"\" id=\"624f0c67e1af860b1c6b99f1\" width=\"\"><\/div>\n<h2>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0413\u043e\u0432\u043e\u0440\u044f \u043f\u043e-\u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443, \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 Android. \u041a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u00a0<a href=\"https:\/\/developer.android.com\/kotlin\/coroutines\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0437\u0430\u0432\u0438\u0441\u0430\u043d\u0438\u044e UI \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b callback-\u043a\u043e\u0434\u0430 \u043d\u0430 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432:<\/p>\n<pre><code class=\"kotlin\">\/\/ Simplified code that only considers the happy path fun loginUser(userId: String, password: String, userResult: Callback&lt;User>) {   \/\/ Async callbacks   userRemoteDataSource.logUserIn { user ->     \/\/ Successful network request     userLocalDataSource.logUserIn(user) { userDb ->       \/\/ Result saved in DB       userResult.success(userDb)     }   } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u0438 \u043a\u043e\u043b\u0431\u0435\u043a\u0438\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d:<\/p>\n<pre><code class=\"kotlin\">suspend fun loginUser(userId: String, password: String): UserDb {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   return userDb }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445, \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e\u00a0<strong>suspend<\/strong>. \u0422\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d. \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0439\u0442\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0443\u044e, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442.<\/p>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432, \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>\u041d\u043e \u0447\u0442\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u043d\u0443\u0442\u0440\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a\u00a0<em>suspend<\/em>?<\/p>\n<h2>Suspend \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>loginUser<\/code>, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435, \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0442\u0430\u043a\u0436\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">suspend fun loginUser(userId: String, password: String): UserDb {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   return userDb }  \/\/ UserRemoteDataSource.kt suspend fun logUserIn(userId: String, password: String): User  \/\/ UserLocalDataSource.kt suspend fun logUserIn(userId: String): UserDb<\/code><\/pre>\n<p>\u041a\u0440\u0430\u0442\u043a\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u0431\u0435\u0440\u0435\u0442 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u0438\u0445 \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Finite-state_machine\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439<\/a>\u00a0(\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0437\u0436\u0435).<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Continuation<\/h3>\n<p>Suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<code>Continuation<\/code>\u00a0\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.\u00a0<code>Continuation<\/code>\u00a0\u043e\u0431\u044a\u0435\u043a\u0442 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 generic \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041f\u043e\u0437\u0436\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.<\/p>\n<p>\u0421\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">interface Continuation&lt;in T> {   public val context: CoroutineContext   public fun resumeWith(value: Result&lt;T>) }<\/code><\/pre>\n<ul>\n<li>\n<p><code>context<\/code>\u00a0\u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u00a0<code>CoroutineContext<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><code>resumeWith<\/code>\u00a0\u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441\u00a0<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/stdlib-stubs\/src\/Result.kt\" rel=\"noopener noreferrer nofollow\">Result<\/a>, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043b\u0438\u0431\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p><em>\u0421 Kotlin 1.3 \u0438 \u0434\u0430\u043b\u0435\u0435, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c extensions \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<\/em><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/resume.html\" rel=\"noopener noreferrer nofollow\"><em>resume(value: T)<\/em><\/a><em>\u00a0\u0438\u00a0<\/em><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/resume-with-exception.html\" rel=\"noopener noreferrer nofollow\"><em>resumeWithException(exception: Throwable)<\/em><\/a><em>, \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<\/em><code>resumeWith<\/code><em>.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e suspend \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u00a0<code>completion<\/code>\u00a0(\u0442\u0438\u043f\u00a0<code>Continuation<\/code>) \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430\u0440\u0433\u0443\u0435\u043c\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   completion.resume(userDb) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>Unit<\/code>\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u00a0<code>User<\/code>.<\/p>\n<p>\u0411\u0430\u0439\u0442\u043a\u043e\u0434 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>Any?<\/code>\u00a0\u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 (union) \u0442\u0438\u043f\u043e\u0432\u00a0<code>T | COROUTINE_SUSPENDED<\/code>. \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<blockquote>\n<p><em>\u0415\u0441\u043b\u0438 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 Continuation, \u043d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441 \u043d\u0438\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0431\u0430\u0439\u0442\u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u00a0<code>Continuation<\/code>\u00a0\u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u043a\u043e\u043b\u0431\u0435\u043a-API \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/suspend-coroutine.html\" rel=\"noopener noreferrer nofollow\">suspendCoroutine<\/a>\u00a0\u0438\u043b\u0438\u00a0<a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/suspend-cancellable-coroutine.html\" rel=\"noopener noreferrer nofollow\">suspendCancellableCoroutine<\/a>\u00a0(\u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432). \u0412\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442\u0435 \u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u043c\u00a0<code>Continuation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443, \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438\u00a0<a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/start-coroutine.html\" rel=\"noopener noreferrer nofollow\">startCoroutine<\/a>\u00a0extension \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 suspend \u043c\u0435\u0442\u043e\u0434\u0435. \u041e\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442\u00a0<code>Continuation<\/code>\u00a0\u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c, \u043b\u0438\u0431\u043e \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c Dispatchers<\/h3>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0430\u043c\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445. \u041a\u0430\u043a Kotlin \u0437\u043d\u0430\u0435\u0442, \u0433\u0434\u0435 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c suspend \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f?<\/p>\n<p>\u0415\u0441\u0442\u044c \u043f\u043e\u0434\u0442\u0438\u043f\u00a0<code>Continuation<\/code>, \u043e\u043d \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/kotlinx-coroutines-core\/common\/src\/internal\/DispatchedContinuation.kt\" rel=\"noopener noreferrer nofollow\">DispatchedContinuation<\/a>, \u0433\u0434\u0435 \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u00a0<code>resume<\/code>\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u00a0<code>Dispatcher<\/code>\u00a0\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b\u00a0<code>CoroutineContext<\/code>. \u0412\u0441\u0435 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u044b (<code>Dispatchers<\/code>) \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u00a0<code>dispatch<\/code>, \u043a\u0440\u043e\u043c\u0435 \u0442\u0438\u043f\u0430\u00a0<code>Dispatchers.Unconfined<\/code>, \u043e\u043d \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u00a0<code>isDispatchNeeded<\/code>\u00a0(\u043e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c\u00a0<code>dispatch<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<em>false<\/em>\u00a0\u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<h2>\u0421\u0433\u0435\u043d\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439<\/h2>\n<blockquote>\n<p><em>\u0423\u0442\u043e\u0447\u043d\u0435\u043d\u0438\u0435: \u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u0430\u0439\u0442\u043a\u043e\u0434\u0443 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u0434 \u043d\u0430 Kotlin, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u0439, \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438. \u042d\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 1.3.3 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438. \u041a\u0430\u0436\u0434\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0422\u0430\u043a\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0442\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {      \/\/ Label 0 -> first execution   val user = userRemoteDataSource.logUserIn(userId, password)      \/\/ Label 1 -> resumes from userRemoteDataSource   val userDb = userLocalDataSource.logUserIn(user)      \/\/ Label 2 -> resumes from userLocalDataSource   completion.resume(userDb) }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>when<\/code>\u00a0\u0434\u043b\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {   when(label) {     0 -> { \/\/ Label 0 -> first execution         userRemoteDataSource.logUserIn(userId, password)     }     1 -> { \/\/ Label 1 -> resumes from userRemoteDataSource         userLocalDataSource.logUserIn(user)     }     2 -> { \/\/ Label 2 -> resumes from userLocalDataSource         completion.resume(userDb)     }     else -> throw IllegalStateException(\/* ... *\/)   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435\u043f\u043e\u043b\u043d\u044b\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u00a0<code>Continuation<\/code>. \u0412\u043e\u0442 \u043f\u043e\u0447\u0435\u043c\u0443 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438 \u0442\u0438\u043f \u0432\u00a0<code>Continuation<\/code>\u00a0\u044d\u0442\u043e\u00a0<code>Any?<\/code>\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430\u00a0<code>User<\/code>.<\/p>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439:<\/p>\n<ol>\n<li>\n<p>\u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00a0<code>loginUser<\/code>\u00a0\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0434\u043b\u044f \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u0438\u0434 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<blockquote>\n<p><em>\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/em><\/p>\n<\/blockquote>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {      class LoginUserStateMachine(     \/\/ completion parameter is the callback to the function      \/\/ that called loginUser     completion: Continuation&lt;Any?>   ): CoroutineImpl(completion) {        \/\/ Local variables of the suspend function     var user: User? = null     var userDb: UserDb? = null        \/\/ Common objects for all CoroutineImpls     var result: Any? = null     var label: Int = 0        \/\/ this function calls the loginUser again to trigger the     \/\/ state machine (label will be already in the next state) and     \/\/ result will be the result of the previous state's computation     override fun invokeSuspend(result: Any?) {       this.result = result       loginUser(null, null, this)     }   }   \/* ... *\/ }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443\u00a0<code>invokeSuspend<\/code>\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u00a0<code>loginUser<\/code>\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c\u00a0<code>Continuation<\/code>, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>loginUser<\/code>\u00a0\u0431\u0443\u0434\u0443\u0442 \u043d\u0443\u043b\u0435\u0432\u044b\u043c\u0438. \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c:<\/p>\n<ol>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0438\u043b\u0438<\/p>\n<\/li>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u00a0<code>Continuation<\/code>\u00a0\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {   \/* ... *\/   val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)   \/* ... *\/ }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u00a0<code>LoginUserStateMachine<\/code>\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u00a0<code>completion<\/code>\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435. \u0418\u043d\u0430\u0447\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043b\u044f \u0441\u043c\u0435\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {     \/* ... *\/      val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)      when(continuation.label) {         0 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Next time this continuation is called, it should go to state 1             continuation.label = 1             \/\/ The continuation object is passed to logUserIn to resume              \/\/ this state machine's execution when it finishes             userRemoteDataSource.logUserIn(userId!!, password!!, continuation)         }         1 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Gets the result of the previous state             continuation.user = continuation.result as User             \/\/ Next time this continuation is called, it should go to state 2             continuation.label = 2             \/\/ The continuation object is passed to logUserIn to resume              \/\/ this state machine's execution when it finishes             userLocalDataSource.logUserIn(continuation.user, continuation)         }           \/* ... leaving out the last state on purpose *\/     } }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c \u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u043a\u043e\u0434\u0430:<\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\u00a0<code>label<\/code>\u00a0\u0438\u0437\u00a0<code>LoginUserStateMachine<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u00a0<code>when<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0435\u0441\u0442\u044c \u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (<code>logUserIn<\/code>),\u00a0<code>LoginUserStateMachine<\/code>\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\u00a0<code>label<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0430\u044f suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u00a0<code>Continuation<\/code>\u00a0(\u0441 \u0442\u0438\u043f\u043e\u043c\u00a0<code>LoginUserStateMachine<\/code>) \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442. \u0412\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u0431\u044b\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0430 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c \u0441\u043e \u0441\u0432\u043e\u0435\u0439 \u043c\u0430\u0448\u0438\u043d\u043e\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u041a\u043e\u0433\u0434\u0430 \u044d\u0442\u0430 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442 \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443, \u043e\u043d\u0430 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u201c\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0439\u201d \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u00a0<code>completion<\/code>\u00a0\u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0437\u043e\u0432\u00a0<code>continuation.cont.resume<\/code>\u00a0(\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0447\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u00a0<code>completion<\/code>, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\u00a0<code>continuation.cont<\/code>\u00a0\u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430\u00a0<code>LoginUserStateMachine<\/code>):<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {     \/* ... *\/      val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)      when(continuation.label) {         \/* ... *\/         2 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Gets the result of the previous state             continuation.userDb = continuation.result as UserDb             \/\/ Resumes the execution of the function that called this one             continuation.cont.resume(continuation.userDb)         }         else -> throw IllegalStateException(\/* ... *\/)     } } <\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u0434\u0435\u043b\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u201c\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c\u201d. \u0418\u0437\u00a0<em>suspend<\/em>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"kotlin\">suspend fun loginUser(userId: String, password: String): User {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   return userDb }<\/code><\/pre>\n<p>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u0443\u0441\u043e\u043a \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {      class LoginUserStateMachine(         \/\/ completion parameter is the callback to the function that called loginUser         completion: Continuation&lt;Any?>     ): CoroutineImpl(completion) {         \/\/ objects to store across the suspend function         var user: User? = null         var userDb: UserDb? = null          \/\/ Common objects for all CoroutineImpl         var result: Any? = null         var label: Int = 0          \/\/ this function calls the loginUser again to trigger the          \/\/ state machine (label will be already in the next state) and          \/\/ result will be the result of the previous state's computation         override fun invokeSuspend(result: Any?) {             this.result = result             loginUser(null, null, this)         }     }      val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)      when(continuation.label) {         0 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Next time this continuation is called, it should go to state 1             continuation.label = 1             \/\/ The continuation object is passed to logUserIn to resume              \/\/ this state machine's execution when it finishes             userRemoteDataSource.logUserIn(userId!!, password!!, continuation)         }         1 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Gets the result of the previous state             continuation.user = continuation.result as User             \/\/ Next time this continuation is called, it should go to state 2             continuation.label = 2             \/\/ The continuation object is passed to logUserIn to resume              \/\/ this state machine's execution when it finishes             userLocalDataSource.logUserIn(continuation.user, continuation)         }         2 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Gets the result of the previous state             continuation.userDb = continuation.result as UserDb             \/\/ Resumes the execution of the function that called this one             continuation.cont.resume(continuation.userDb)         }         else -> throw IllegalStateException(\/* ... *\/)     } }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u0443\u044e\u00a0<em>suspend<\/em>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u043c\u0430\u0448\u0438\u043d\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u0417\u043d\u0430\u044f \u043a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u201c\u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c\u201d, \u0432\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u0442\u0435:<\/p>\n<ul>\n<li>\n<p>\u043f\u043e\u0447\u0435\u043c\u0443\u00a0<em>suspend<\/em>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0432\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u043d\u0430 \u043d\u0430\u0447\u0430\u043b\u0430;<\/p>\n<\/li>\n<li>\n<p>\u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043a\u043e\u0434 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044f \u043f\u043e\u0442\u043e\u043a\u0438 (\u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u043e \u0442\u043e\u043c \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u044b, \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u0435\u00a0<code>Continuation<\/code>).<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/659699\/\"> https:\/\/habr.com\/ru\/post\/659699\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u00a0<em>suspend<\/em>\u00a0\u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c?<\/p>\n<p>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0432 Kotlin \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u043e\u043c\u00a0<strong><em>suspend<\/em><\/strong>. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438? \u041a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u00a0<em>suspend<\/em>\u00a0\u0431\u043b\u043e\u043a\u0438 \u0432 \u043a\u043e\u0434, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b?<\/p>\n<p>\u0417\u043d\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0432\u0441\u044f \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0438 \u043a\u0430\u043a \u043a\u043e\u0434 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432.<\/p>\n<blockquote>\n<p><em>TL;DR; \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u044d\u0442\u0430 \u043c\u0430\u0448\u0438\u043d\u0430 \u0431\u0435\u0440\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u043e\u0439 \u043d\u0430 \u0441\u0435\u0431\u044f!<\/em><\/p>\n<\/blockquote>\n<p>\u041d\u043e\u0432\u0435\u043d\u044c\u043a\u0438\u0439 \u0432 Android? \u0412\u0437\u0433\u043b\u044f\u043d\u0438 \u043d\u0430 \u044d\u0442\u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043f\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/codelabs.developers.google.com\/codelabs\/kotlin-coroutines\/#0\" rel=\"noopener noreferrer nofollow\">Using coroutines in your Android app<\/a>.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.android.com\/codelabs\/advanced-kotlin-coroutines#0\" rel=\"noopener noreferrer nofollow\">Advanced Coroutines with Kotlin Flow and Live Data<\/a>.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0438\u0434\u0435\u043e:<\/p>\n<div class=\"tm-iframe_temp\" data-src=\"https:\/\/embedd.srv.habr.com\/iframe\/624f0c67e1af860b1c6b99f1\" data-style=\"\" id=\"624f0c67e1af860b1c6b99f1\" width=\"\"><\/div>\n<h2>\u041a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0413\u043e\u0432\u043e\u0440\u044f \u043f\u043e-\u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443, \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 Android. \u041a\u0430\u043a \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432\u00a0<a href=\"https:\/\/developer.android.com\/kotlin\/coroutines\" rel=\"noopener noreferrer nofollow\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0437\u0430\u0432\u0438\u0441\u0430\u043d\u0438\u044e UI \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b callback-\u043a\u043e\u0434\u0430 \u043d\u0430 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432:<\/p>\n<pre><code class=\"kotlin\">\/\/ Simplified code that only considers the happy path fun loginUser(userId: String, password: String, userResult: Callback&lt;User>) {   \/\/ Async callbacks   userRemoteDataSource.logUserIn { user ->     \/\/ Successful network request     userLocalDataSource.logUserIn(user) { userDb ->       \/\/ Result saved in DB       userResult.success(userDb)     }   } }<\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u0438 \u043a\u043e\u043b\u0431\u0435\u043a\u0438\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d:<\/p>\n<pre><code class=\"kotlin\">suspend fun loginUser(userId: String, password: String): UserDb {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   return userDb }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445, \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e\u00a0<strong>suspend<\/strong>. \u0422\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d. \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0439\u0442\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0443\u044e, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442.<\/p>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432, \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>\u041d\u043e \u0447\u0442\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u043d\u0443\u0442\u0440\u0438, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043a\u0430\u043a\u00a0<em>suspend<\/em>?<\/p>\n<h2>Suspend \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/h2>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u043a suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>loginUser<\/code>, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435, \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0442\u0430\u043a\u0436\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">suspend fun loginUser(userId: String, password: String): UserDb {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   return userDb }  \/\/ UserRemoteDataSource.kt suspend fun logUserIn(userId: String, password: String): User  \/\/ UserLocalDataSource.kt suspend fun logUserIn(userId: String): UserDb<\/code><\/pre>\n<p>\u041a\u0440\u0430\u0442\u043a\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u0431\u0435\u0440\u0435\u0442 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 \u0438\u0445 \u0432 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Finite-state_machine\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439<\/a>\u00a0(\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0437\u0436\u0435).<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Continuation<\/h3>\n<p>Suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<code>Continuation<\/code>\u00a0\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.\u00a0<code>Continuation<\/code>\u00a0\u043e\u0431\u044a\u0435\u043a\u0442 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 generic \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041f\u043e\u0437\u0436\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0434\u043b\u044f suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.<\/p>\n<p>\u0421\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"kotlin\">interface Continuation&lt;in T> {   public val context: CoroutineContext   public fun resumeWith(value: Result&lt;T>) }<\/code><\/pre>\n<ul>\n<li>\n<p><code>context<\/code>\u00a0\u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u00a0<code>CoroutineContext<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p><code>resumeWith<\/code>\u00a0\u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441\u00a0<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/stdlib-stubs\/src\/Result.kt\" rel=\"noopener noreferrer nofollow\">Result<\/a>, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043b\u0438\u0431\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p><em>\u0421 Kotlin 1.3 \u0438 \u0434\u0430\u043b\u0435\u0435, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c extensions \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<\/em><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/resume.html\" rel=\"noopener noreferrer nofollow\"><em>resume(value: T)<\/em><\/a><em>\u00a0\u0438\u00a0<\/em><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/resume-with-exception.html\" rel=\"noopener noreferrer nofollow\"><em>resumeWithException(exception: Throwable)<\/em><\/a><em>, \u044d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u00a0<\/em><code>resumeWith<\/code><em>.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e suspend \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u00a0<code>completion<\/code>\u00a0(\u0442\u0438\u043f\u00a0<code>Continuation<\/code>) \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430\u0440\u0433\u0443\u0435\u043c\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {   val user = userRemoteDataSource.logUserIn(userId, password)   val userDb = userLocalDataSource.logUserIn(user)   completion.resume(userDb) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043d\u0430\u0448 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>Unit<\/code>\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u00a0<code>User<\/code>.<\/p>\n<p>\u0411\u0430\u0439\u0442\u043a\u043e\u0434 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>Any?<\/code>\u00a0\u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 (union) \u0442\u0438\u043f\u043e\u0432\u00a0<code>T | COROUTINE_SUSPENDED<\/code>. \u0427\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<blockquote>\n<p><em>\u0415\u0441\u043b\u0438 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 Continuation, \u043d\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441 \u043d\u0438\u043c \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0431\u0430\u0439\u0442\u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u00a0<code>Continuation<\/code>\u00a0\u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432:<\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u0438 \u043a\u043e\u043b\u0431\u0435\u043a-API \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/suspend-coroutine.html\" rel=\"noopener noreferrer nofollow\">suspendCoroutine<\/a>\u00a0\u0438\u043b\u0438\u00a0<a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/suspend-cancellable-coroutine.html\" rel=\"noopener noreferrer nofollow\">suspendCancellableCoroutine<\/a>\u00a0(\u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432). \u0412\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442\u0435 \u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u043c\u00a0<code>Continuation<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443, \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0438\u0437 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 suspend \u0444\u0443\u043d\u043a\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438\u00a0<a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/start-coroutine.html\" rel=\"noopener noreferrer nofollow\">startCoroutine<\/a>\u00a0extension \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 suspend \u043c\u0435\u0442\u043e\u0434\u0435. \u041e\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442\u00a0<code>Continuation<\/code>\u00a0\u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d, \u043a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c, \u043b\u0438\u0431\u043e \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c.<\/p>\n<\/li>\n<\/ul>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c Dispatchers<\/h3>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u0430\u043c\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445. \u041a\u0430\u043a Kotlin \u0437\u043d\u0430\u0435\u0442, \u0433\u0434\u0435 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c suspend \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f?<\/p>\n<p>\u0415\u0441\u0442\u044c \u043f\u043e\u0434\u0442\u0438\u043f\u00a0<code>Continuation<\/code>, \u043e\u043d \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\u00a0<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/kotlinx-coroutines-core\/common\/src\/internal\/DispatchedContinuation.kt\" rel=\"noopener noreferrer nofollow\">DispatchedContinuation<\/a>, \u0433\u0434\u0435 \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u00a0<code>resume<\/code>\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u00a0<code>Dispatcher<\/code>\u00a0\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0433\u043e \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b\u00a0<code>CoroutineContext<\/code>. \u0412\u0441\u0435 \u0434\u0438\u0441\u043f\u0435\u0442\u0447\u0435\u0440\u044b (<code>Dispatchers<\/code>) \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u00a0<code>dispatch<\/code>, \u043a\u0440\u043e\u043c\u0435 \u0442\u0438\u043f\u0430\u00a0<code>Dispatchers.Unconfined<\/code>, \u043e\u043d \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u00a0<code>isDispatchNeeded<\/code>\u00a0(\u043e\u043d \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c\u00a0<code>dispatch<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<em>false<\/em>\u00a0\u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<h2>\u0421\u0433\u0435\u043d\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u0430\u0448\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439<\/h2>\n<blockquote>\n<p><em>\u0423\u0442\u043e\u0447\u043d\u0435\u043d\u0438\u0435: \u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0431\u0430\u0439\u0442\u043a\u043e\u0434\u0443 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u043e\u0434 \u043d\u0430 Kotlin, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u0439, \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0438. \u042d\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 1.3.3 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/em><\/p>\n<\/blockquote>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Kotlin \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438. \u041a\u0430\u0436\u0434\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0422\u0430\u043a\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0442\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {      \/\/ Label 0 -> first execution   val user = userRemoteDataSource.logUserIn(userId, password)      \/\/ Label 1 -> resumes from userRemoteDataSource   val userDb = userLocalDataSource.logUserIn(user)      \/\/ Label 2 -> resumes from userLocalDataSource   completion.resume(userDb) }<\/code><\/pre>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>when<\/code>\u00a0\u0434\u043b\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String, password: String, completion: Continuation&lt;Any?>) {   when(label) {     0 -> { \/\/ Label 0 -> first execution         userRemoteDataSource.logUserIn(userId, password)     }     1 -> { \/\/ Label 1 -> resumes from userRemoteDataSource         userLocalDataSource.logUserIn(user)     }     2 -> { \/\/ Label 2 -> resumes from userLocalDataSource         completion.resume(userDb)     }     else -> throw IllegalStateException(\/* ... *\/)   } }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435\u043f\u043e\u043b\u043d\u044b\u0439, \u0442\u0430\u043a \u043a\u0430\u043a \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0431\u043c\u0435\u043d\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u00a0<code>Continuation<\/code>. \u0412\u043e\u0442 \u043f\u043e\u0447\u0435\u043c\u0443 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438 \u0442\u0438\u043f \u0432\u00a0<code>Continuation<\/code>\u00a0\u044d\u0442\u043e\u00a0<code>Any?<\/code>\u00a0\u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430\u00a0<code>User<\/code>.<\/p>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439:<\/p>\n<ol>\n<li>\n<p>\u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00a0<code>loginUser<\/code>\u00a0\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0434\u043b\u044f \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u0432\u0438\u0434 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<blockquote>\n<p><em>\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0434\u043b\u044f \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439<\/em><\/p>\n<\/blockquote>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {      class LoginUserStateMachine(     \/\/ completion parameter is the callback to the function      \/\/ that called loginUser     completion: Continuation&lt;Any?>   ): CoroutineImpl(completion) {        \/\/ Local variables of the suspend function     var user: User? = null     var userDb: UserDb? = null        \/\/ Common objects for all CoroutineImpls     var result: Any? = null     var label: Int = 0        \/\/ this function calls the loginUser again to trigger the     \/\/ state machine (label will be already in the next state) and     \/\/ result will be the result of the previous state's computation     override fun invokeSuspend(result: Any?) {       this.result = result       loginUser(null, null, this)     }   }   \/* ... *\/ }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443\u00a0<code>invokeSuspend<\/code>\u00a0\u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u00a0<code>loginUser<\/code>\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c\u00a0<code>Continuation<\/code>, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>loginUser<\/code>\u00a0\u0431\u0443\u0434\u0443\u0442 \u043d\u0443\u043b\u0435\u0432\u044b\u043c\u0438. \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043a\u0430\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0435.<\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c:<\/p>\n<ol>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0438\u043b\u0438<\/p>\n<\/li>\n<li>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u00a0<code>Continuation<\/code>\u00a0\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {   \/* ... *\/   val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)   \/* ... *\/ }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u00a0<code>LoginUserStateMachine<\/code>\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u00a0<code>completion<\/code>\u00a0\u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435. \u0418\u043d\u0430\u0447\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0434\u043b\u044f \u0441\u043c\u0435\u043d\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438:<\/p>\n<pre><code class=\"kotlin\">fun loginUser(userId: String?, password: String?, completion: Continuation&lt;Any?>) {     \/* ... *\/      val continuation = completion as? LoginUserStateMachine ?: LoginUserStateMachine(completion)      when(continuation.label) {         0 -> {             \/\/ Checks for failures             throwOnFailure(continuation.result)             \/\/ Next time this continuation is called, it should go to state 1             continuation.label = 1             \/\/ The continuation object is passed to logUserIn to resume        <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-331660","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331660"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/331660\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}