Promise.Then()返回Promise{<Pending>}

函数fetchData返回一个Promise,然后我通过将promise.then链接在一起来处理这个Promise,但它返回Promise<;Pending&>。函数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));

相关文章