函数 fetchData 返回一个承诺,然后我通过链接在generateURL函数中处理该承诺promise
with .then
但是它返回 Promise 。函数generateURL应该返回一个字符串我做错了什么?
const fetch = require('node-fetch');
const fetchData = async () => {
return await fetch('https://jsonplaceholder.typicode.com/todos/1');
};
const generateURL = () => {
const baseURL = 'https://cdn.test.com/';
fetchData().then((res) => {
const data = res.json();
console.log('data', data);
const id = data.id;
console.log('id', id);
const generatedURL = `${baseURL}${id}`;
return generatedURL;
});
};
减少了一点代码,而不是添加多个承诺解析
const fetch = require('node-fetch');
const everything = async () =>{
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const {id} = await response.json();
return `https://cdn.test.com/${id}`
}
everything().then((res)=>console.log(res));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)