我使用https://github.com/infinitered/apisauce来获取某些数据,但我无法将收到的内容传输到导入此功能的模块。
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 :(得分:0)
const handler = {
get(target, propKey, receiver) {
if (propKey != util.inspect.custom &&
propKey != 'inspect' &&
propKey != Symbol.toStringTag){
if (target.ready == false) {
throw new Error('not ready');
} else {
return 'ready'
}
}
}
};
典型的异步问题。我更喜欢使用ES7支持的异步
let result
getter().then(res=> result = res)
答案 1 :(得分:0)
这有效:
<强> consumer.js 强>
import getter from './apisause-get';
let result;
getter.then( (result) => {
console.log('==================');
console.log('we got result: ', result); // Works! :)
})