2017-10-25 10 views
0

私はhttps://github.com/infinitered/apisauceを使用してデータを取得していますが、受信したデータをこの機能をインポートするモジュールに転送することはできません。API呼び出しのデータを返す約束を解決できません

apisause-get.js

1 import {create} from 'apisauce' 
    2 
    3 const api = create({ 
    4 baseURL: 'https://gist.githubusercontent.com', 
    5 }) 
    6 
    7 export default api 
    8 .get('/stabenfeldt/14db8a93d8bffd5258e29c03e2dfe234/raw/207c7facfa8ad5c1cce20ed08d954dad4a8ee75e/token.json') 
    9 .then((response) => response.data) 
10 //.then(console.log) // outputs correct data here 
11 

consumer.js

1 import getter from './apisause-get.js'; 
    2 
    3 let result = getter(); 
    4 
    5 console.log('=================='); 
    6 console.log('we got result: ', result); // I'm not getting the result from the api. 

babel-node consumer.jsは何も返しません。

答えて

0
let result 
getter().then(res=> result = res) 

典型的な非同期問題です。私はこの作品ES7

let getData = async() => { 
    result = await getter() 
} 
getData() 
+0

をこんにちは、私はconsumer.jsにあなたの最後のコードブロックを追加しましたが、doesnのこと仕事はありません。 'we got result:undefined' – martins

0

でサポートされている非同期使用することを好む:

consumer.js

import getter from './apisause-get'; 

let result; 

getter.then((result) => { 
    console.log('=================='); 
    console.log('we got result: ', result); // Works! :) 
}) 
+0

モジュールスコープで 'let result'を宣言する必要はありません – Bergi

関連する問題