gpt4 book ai didi

javascript - Promise.then() 返回 Promise { }

转载 作者:行者123 更新时间:2023-12-02 18:59:39 27 4
gpt4 key购买 nike

函数 fetchData 返回一个 promise ,然后我通过链接 promise 在generateURL函数中处理该 promise 。与 .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;
});
};

最佳答案

减少了一点代码,而不是添加多个 promise 解析

  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));

关于javascript - Promise.then() 返回 Promise { <pending> },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65755094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com