moelholm
03/18/2017, 9:02 PMmoelholm
03/18/2017, 9:03 PMmoelholm
03/18/2017, 9:14 PMmoelholm
03/18/2017, 9:14 PMkenkyee
03/19/2017, 12:16 PMgaetan
03/20/2017, 9:56 AMkonsoletyper
03/20/2017, 9:58 AMgaetan
03/20/2017, 10:03 AMgaetan
03/20/2017, 10:05 AMkonsoletyper
03/20/2017, 10:08 AMpublic fun <T> async(c: suspend () -> T): Promise<T> {
return Promise { resolve, reject ->
c.startCoroutine(object : Continuation<T> {
override fun resume(value: T) = resolve(value)
override fun resumeWithException(exception: Throwable) = reject(exception)
override val context = EmptyCoroutineContext
})
}
}
public suspend fun <T> Promise<T>.await() = suspendCoroutine<T> { c ->
then({ c.resume(it) }, { c.resumeWithException(it) })
}
hackerham
03/20/2017, 10:08 AMkonsoletyper
03/20/2017, 10:09 AMasync {
// do something
val ajaxResponse = fetch(...).await()
// do something
}
konsoletyper
03/20/2017, 10:10 AMPromise
. You can even create specialized await()
functions for XHR, IndexedDB, and so forthgaetan
03/20/2017, 10:10 AMgroostav
03/20/2017, 11:21 PMbreak()
in a for-each loop, that would simply halt the traversal and return to the caller.groostav
03/20/2017, 11:22 PMgroostav
03/20/2017, 11:22 PMbreak()
method does nothing but set a flag in the traversal logic, setting it not to continue.
If I instead made this method be a suspend val
and updated our source such that the visit
methods were all suspend funs
then I can get this:
suspend val Visitor.break get() = Helpers.break(this)
override suspend fun visitEnter(node: NodeTypeTwo){
if(node.modelB.isSpecialCase()){
break //treated as a property-get
}
//domain logic b, never called when the above if-clause is taken as per the `break` suspension
}
groostav
03/20/2017, 11:22 PMnote of course that `suspend val`s do not yet exist, but im hoping their behaviour is fairly obvious
groostav
03/20/2017, 11:25 PMgoto
?ilya.gorbunov
03/21/2017, 1:25 AMsuspendCoroutine
calls, no Continuation.resume
etc.groostav
03/21/2017, 3:24 AMbreak
like functionalitykevinherron
03/21/2017, 3:25 AMgroostav
03/21/2017, 3:26 AMgroostav
03/21/2017, 3:26 AMgroostav
03/21/2017, 3:26 AMorangy
uhe
03/21/2017, 12:40 PMprivate suspend fun foo(): Int {
null?.let {
return bar() // Couldn't inline method call 'let'
}
return 23
}
private suspend fun bar(): Int {
return 42
}
uhe
03/21/2017, 12:40 PMorangy
denis.zharkov
03/21/2017, 1:06 PM