.then()节点模块不工作?

时间:2017-05-29 15:56:56

标签: javascript node.js reactjs react-native module

我现在正在开发一个本机应用程序,我编写了自己的模块来存储与我的组件分开的一些信息。

当我这样做时,我注意到在我创建的模块中,我有一个这样的函数:

testFetch = function(url){
return fetch(url);
}

exports.test = testFetch;

在我的主要模块中,我做了以下事情:

import ts from 'test-module'

ts.test("http://example.com").then((response) => {console.log(response)});

但.then()因任何原因都没有被解雇。请求通过并成功。

对此有何帮助?

1 个答案:

答案 0 :(得分:1)

问题在于您混合CommonJS和ES6模块的方式。您似乎希望示例主模块中的ts为您提供模块依赖项中整个export的值,但这不是它的工作原理。

您可以使用export default导出testFetch功能,如下所示:

testFetch = function (url) {
    return fetch(url);
}

export default testFetch;

主要模块:

import testFetch from 'test-module';

testFetch("http://example.com").then((response) => {console.log(response)});
相关问题