Home > Blockchain >  Getting Promise<pending> in console
Getting Promise<pending> in console

Time:01-04

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!

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

CodePudding user response:

Since getsongs is also async function, it will return a promise, and thus when you print finaldata, it consoles 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.

CodePudding user response:

I believe you should change this line

export const finaldata = getsongs();

to

export const finaldata = await getsongs();

CodePudding user response:

Apparently, you are trying to consume the result of the async operation before it's even finished

  • Related