Skip to content

Instantly share code, notes, and snippets.

View chaxiu's full-sized avatar
🎯
Focusing

BoyCoder chaxiu

🎯
Focusing
View GitHub Profile
val user = getUserInfo()
val friendList = getFriendList(user)
val feedList = getFeedList(friendList)
getUserInfo(new CallBack() {
@Override
public void onSuccess(String response) {
if (response != null) {
System.out.println(response);
}
}
});
getUserInfo(new CallBack() {
@Override
public void onSuccess(String user) {
if (user != null) {
System.out.println(user);
getFriendList(user, new CallBack() {
@Override
public void onSuccess(String friendList) {
if (friendList != null) {
System.out.println(friendList);
// delay(1000L) representing request to server
//Suspending Function
// ↓
suspend fun getUserInfo(): String {
withContext(Dispatchers.IO) {
delay(1000L)
}
return "BoyCoder"
}
suspend fun getUserInfo(): String {
withContext(Dispatchers.IO) {
delay(1000L)
}
return "BoyCoder"
}
// Continuation is nothing but CallBack
// ↓
public static final Object getUserInfo(Continuation $completion) {
...
return "BoyCoder";
}
public interface Continuation<in T> {
public val context: CoroutineContext
// just like: onSuccess(result)
// ↓ ↓
public fun resumeWith(result: Result<T>)
}
interface CallBack {
void onSuccess(String response);
}
suspend fun getUserInfo(): String {
withContext(Dispatchers.IO) {
delay(1000L)
}
return "BoyCoder"
}
// suspend modifier
// ↓
suspend fun noSuspendFriendList(user: String): String{
// body is just like normal function
return "Tom, Jack"
}