This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getUserInfo(completion: Continuation<Any?>): Any?{} | |
fun getFriendList(user: String, completion: Continuation<Any?>): Any?{} | |
fun getFeedList(friendList: String, completion: Continuation<Any?>): Any?{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun testCoroutine(completion: Continuation<Any?>): Any? {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun testCoroutine() { | |
log("start") | |
val user = getUserInfo() | |
log(user) | |
val friendList = getFriendList(user) | |
log(friendList) | |
val feedList = getFeedList(friendList) | |
log(feedList) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// suspend modifier | |
// ↓ | |
suspend fun noSuspendFriendList(user: String): String{ | |
// body is just like normal function | |
return "Tom, Jack" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun getUserInfo(): String { | |
withContext(Dispatchers.IO) { | |
delay(1000L) | |
} | |
return "BoyCoder" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface CallBack { | |
void onSuccess(String response); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Continuation<in T> { | |
public val context: CoroutineContext | |
// just like: onSuccess(result) | |
// ↓ ↓ | |
public fun resumeWith(result: Result<T>) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Continuation is nothing but CallBack | |
// ↓ | |
public static final Object getUserInfo(Continuation $completion) { | |
... | |
return "BoyCoder"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun getUserInfo(): String { | |
withContext(Dispatchers.IO) { | |
delay(1000L) | |
} | |
return "BoyCoder" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// delay(1000L) representing request to server | |
//Suspending Function | |
// ↓ | |
suspend fun getUserInfo(): String { | |
withContext(Dispatchers.IO) { | |
delay(1000L) | |
} | |
return "BoyCoder" | |
} |