Promise.Then()返回Promise{<;Pending>;}
原学程将引见Promise.Then()前往Promise{<;Pending>;}的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
函数fetchData前往1个Promise,而后我经由过程将promise
与.then
链交在一路去处置这个Promise,但是它前往Promise<;Pending&>。函数GenerateURL应前往1个字符串?我做错了甚么?
const fetch = require('node-fetch');
const fetchData = async () => {
return await fetch('https://jsonplaceholder.typicode.com/todos/一');
};
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;
});
};
推举谜底
削减了1面代码,而没有是添减多个许诺剖析
const fetch = require('node-fetch');
const everything = async () =>{
const response = await fetch('https://jsonplaceholder.typicode.com/todos/一');
const {id} = await response.json();
return `https://cdn.test.com/${id}`
}
everything().then((res)=>console.log(res));
佳了闭于Promise.Then()前往Promise{<;Pending>;}的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。