{"id":513,"date":"2025-06-18T17:43:59","date_gmt":"2025-06-18T09:43:59","guid":{"rendered":"https:\/\/trantor.ink\/?p=513"},"modified":"2025-06-18T17:44:00","modified_gmt":"2025-06-18T09:44:00","slug":"kotlin-%e5%8d%8f%e7%a8%8b%e6%80%8e%e4%b9%88%e5%b0%86%e5%bc%82%e6%ad%a5%e5%9b%9e%e8%b0%83%e5%86%85%e5%ae%b9%e5%90%8c%e6%ad%a5-return-%e5%87%ba%e5%8e%bb","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=513","title":{"rendered":"kotlin \u534f\u7a0b\u600e\u4e48\u5c06\u5f02\u6b65\u56de\u8c03\u5185\u5bb9\u540c\u6b65 return \u51fa\u53bb"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=513\/#%E4%BD%BF%E7%94%A8CompletableDeferred\" >\u4f7f\u7528CompletableDeferred<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=513\/#%E4%BD%BF%E7%94%A8suspendCancellableCoroutine\" >\u4f7f\u7528suspendCancellableCoroutine<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>\u5728Kotlin\u4e2d\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u534f\u7a0b\u4e2d\u5c06\u5f02\u6b65\u56de\u8c03\u7684\u7ed3\u679c\u540c\u6b65\u8fd4\u56de\uff0c\u53ef\u4ee5\u4f7f\u7528<code>CompletableDeferred<\/code>\u6216<code>suspendCancellableCoroutine<\/code>\u7b49\u5de5\u5177\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8CompletableDeferred\"><\/span>\u4f7f\u7528<code>CompletableDeferred<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>CompletableDeferred<\/code>\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86<code>Deferred<\/code>\u63a5\u53e3\u7684\u7c7b\uff0c\u5141\u8bb8\u4f60\u624b\u52a8\u5b8c\u6210\u5176\u7ed3\u679c\u3002\u8fd9\u5bf9\u4e8e\u4ece\u56de\u8c03\u4e2d\u83b7\u53d6\u7ed3\u679c\u5e76\u8fd4\u56de\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import kotlinx.coroutines.*\n\nfun main() = runBlocking {\n    val result = asyncOperationWithCallback()\n    println(\"Result: $result\")\n}\n\nsuspend fun asyncOperationWithCallback(): String {\n    val deferred = CompletableDeferred&lt;String>()\n\n    \/\/ \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c\n    asyncOperation { result ->\n        deferred.complete(result)\n    }\n\n    return deferred.await()\n}\n\nfun asyncOperation(callback: (String) -> Unit) {\n    \/\/ \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\n    GlobalScope.launch {\n        delay(1000) \/\/ \u6a21\u62df\u5ef6\u8fdf\n        callback(\"Hello from callback\")\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8suspendCancellableCoroutine\"><\/span>\u4f7f\u7528<code>suspendCancellableCoroutine<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>suspendCancellableCoroutine<\/code>\u53ef\u4ee5\u5c06\u5f02\u6b65\u56de\u8c03\u8f6c\u6362\u4e3a\u6302\u8d77\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff0c\u5e76\u5728\u56de\u8c03\u4e2d\u6062\u590d\u5b83\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import kotlinx.coroutines.*\nimport kotlin.coroutines.resume\nimport kotlin.coroutines.resumeWithException\n\nfun main() = runBlocking {\n    try {\n        val result = asyncOperationWithSuspendCoroutine()\n        println(\"Result: $result\")\n    } catch (e: Exception) {\n        println(\"Error: ${e.message}\")\n    }\n}\n\nsuspend fun asyncOperationWithSuspendCoroutine(): String {\n    return suspendCancellableCoroutine { continuation ->\n        \/\/ \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c\n        asyncOperation { result ->\n            if (result != null) {\n                continuation.resume(result)\n            } else {\n                continuation.resumeWithException(Exception(\"Error in async operation\"))\n            }\n        }\n    }\n}\n\nfun asyncOperation(callback: (String?) -> Unit) {\n    \/\/ \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\n    GlobalScope.launch {\n        delay(1000) \/\/ \u6a21\u62df\u5ef6\u8fdf\n        callback(\"Hello from callback\")\n    }\n}<\/pre>\n\n\n\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u5f02\u6b65\u56de\u8c03\u5185\u5bb9\u540c\u6b65\u8fd4\u56de\uff0c\u5e76\u4e14\u5728Kotlin\u534f\u7a0b\u4e2d\u90fd\u975e\u5e38\u5e38\u7528\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u98ce\u683c\u504f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Kotlin\u4e2d\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u534f\u7a0b\u4e2d\u5c06\u5f02\u6b65\u56de\u8c03\u7684\u7ed3\u679c\u540c\u6b65\u8fd4\u56de\uff0c\u53ef\u4ee5\u4f7f\u7528CompletableDeferred\u6216suspendCancellableCoroutine\u7b49\u5de5\u5177\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002 \u4f7f\u7528CompletableDeferred CompletableDeferred\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86Deferred\u63a5\u53e3\u7684\u7c7b\uff0c\u5141\u8bb8\u4f60\u624b\u52a8\u5b8c\u6210\u5176\u7ed3\u679c\u3002\u8fd9\u5bf9\u4e8e\u4ece\u56de\u8c03\u4e2d\u83b7\u53d6\u7ed3\u679c\u5e76\u8fd4\u56de\u975e\u5e38\u6709\u7528\u3002 \u4f7f\u7528suspendCancellableCoroutine suspendCancellableCoroutine\u53ef\u4ee5\u5c06\u5f02\u6b65\u56de\u8c03\u8f6c\u6362\u4e3a\u6302\u8d77\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u6302\u8d77\u5f53\u524d\u534f\u7a0b\uff0c\u5e76\u5728\u56de\u8c03\u4e2d\u6062\u590d\u5b83\u3002 \u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u5f02\u6b65\u56de\u8c03\u5185\u5bb9\u540c\u6b65\u8fd4\u56de\uff0c\u5e76\u4e14\u5728Kotlin\u534f\u7a0b\u4e2d\u90fd\u975e\u5e38\u5e38\u7528\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4ee3\u7801\u98ce\u683c\u504f\u597d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7],"tags":[61,63],"class_list":["post-513","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-appdev","tag-kotlin","tag-63"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=513"}],"version-history":[{"count":1,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/513\/revisions\/514"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}