如何导入基于承诺的变量以在另一个模块(javascript)中使用?
如何将基于承诺的函数的值导入另一个模块如何导入基于承诺的变量以在另一个模块(javascript)中使用?,javascript,module,fetch,es6-promise,Javascript,Module,Fetch,Es6 Promise,如何将基于承诺的函数的值导入另一个模块 // index.js file: import test from "./test"; console.log(test) // expected result should be "delectus aut autem" - now getting Promise object // test.js file: var test = fetch('https://jsonplaceholder.typicode.com/todos/1')
// index.js file:
import test from "./test";
console.log(test) // expected result should be "delectus aut autem" - now getting Promise object
// test.js file:
var test = fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(function (data) {
return data.title
});
export default test
工作示例:
既然
测试
是一个承诺,您只需在导入后将其作为承诺进行处理即可
test.then(data => {
console.log(data);
});
这可能只是使用
然后处理承诺的问题。例如,test.then(data=>{console.log(data)})
太好了,如果您不介意投票/接受的话,我会将其形式化为一个答案