简体   繁体   English

获取 Promise<pending> 在控制台</pending>

[英]Getting Promise<pending> in console

It is a React (Ts) project and I am getting a Promise<Pending> problem when I try to get request in Axios. Thank you in advance for your help!这是一个 React (Ts) 项目,当我尝试在 Axios 中获取请求时遇到Promise<Pending>问题。提前感谢您的帮助!

import axios from 'axios';

interface song {
  name: string;
}

interface playlist {
  playlistname: string;
  songs: song[];
}

async function getsongs(): Promise<song[]> {
  const url = 'https://be-ask.tanaypratap.repl.co/playlist';
  const response = await axios.get<playlist>(url);
  return response.data.songs;
}

export const finaldata = getsongs();

Since getsongs is also async function, it will return a promise, and thus when you print finaldata, it consoles Promise.由于 getsongs 也是异步的 function,它将返回 promise,因此当您打印最终数据时,它会控制 Promise。

Not sure what you are trying to achieve but if you just want to print the data, print after the await in the function getsongs.不确定您要实现的目标,但如果您只想打印数据,请在等待 function getsongs 后打印。

I believe you should change this line我相信你应该改变这一行

export const finaldata = getsongs();

to

export const finaldata = await getsongs();

Apparently, you are trying to consume the result of the async operation before it's even finished显然,您正试图在异步操作完成之前使用它的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM