2017-01-01 2 views
1

私はkotlin 私のコードでAPIをフェッチ実装する方法を、理解していない:フェッチAPIをどのように実装しますか?

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") 
     }) 

そして、それがすべてでは動作しませんが。いいえコンソールメッセージはなく、任意の

答えて

3

私の推測では、問題はあなたの最初のラムダの中にあったということです。

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

それはと等価であるため、このコードは、何もしません。

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

TL; DRコードを修正するには、第2のカッコのペアを削除します。

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

はい、私はラムダの前に "走っていなかった"ので、解決策の1つが実行される{console.log( "response")} –

+0

うん、それも。その「実行」は空のコードです – voddan

関連する問題