简体   繁体   中英

How do you implement the fetch api?

I don't understand, how to implement fetch api in kotlin my code:

var smf: dynamic = js("({})")
smf.method = "GET"
smf.mode   = "cors"
smf.cache  = "default"

window.fetch(url, smf)
        .then({response -> {
            console.log("response")
        }})
        .catch({error ->
            console.error("error")
        })

And it doesn't work at all. No console messages and any

My guess is that the problem was inside your first lambda:

.then({response -> {
    console.log("response")
}})

This code doesn't do anything, because it is equivalent to:

.then(fun(response: dynamic){
    return {console.log("response")}  // creates a lambda and returns it for no reason
 })

TL;DR To fix the code remove the second pair of braces:

.then {response -> console.log("response")}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM