Javascript 在React中努力实现承诺和异步函数
我正在做一个React项目,我有一个递归获取分页数据的函数。此函数在另一个函数中定义,在该函数中,我在开始时使用Javascript 在React中努力实现承诺和异步函数,javascript,reactjs,Javascript,Reactjs,我正在做一个React项目,我有一个递归获取分页数据的函数。此函数在另一个函数中定义,在该函数中,我在开始时使用showLoading()激活加载屏幕,在主体中调用获取数据函数,并在结束时使用hideLoading()禁用加载屏幕。职能: const fetchPlaylist = (playlistId) => { showLoading() const getPlaylistDataRecursively = (url) => { fetch('/sp
showLoading()
激活加载屏幕,在主体中调用获取数据函数,并在结束时使用hideLoading()
禁用加载屏幕。职能:
const fetchPlaylist = (playlistId) => {
showLoading()
const getPlaylistDataRecursively = (url) => {
fetch('/spotify/get-track-ids', {headers: {
'url': url
}})
.then(response => response.json())
.then(data => {
console.log(data)
setTitles(data.title)
setArtists(data.artist)
setFeatures(data.features)
setIds(data.track_ids)
if (data.next_url) {
const next_url = data.next_url.replace('https://api.spotify.com/v1', '')
getPlaylistDataRecursively(next_url)
}
})
}
getPlaylistDataRecursively(`/playlists/${playlistId}/tracks/?offset=0&limit=100`)
hideLoading()
}
我相信我可以使用
then
关键字递归地向getplaylata调用附加一个承诺,但是我得到了一个TypeError:无法读取未定义的的属性'then'。可能是因为GetPlayDataRecursive不返回任何内容。如何确保在以递归方式获取播放数据后调用hideLoading
const fetchPlaylist = (playlistId) => {
showLoading()
const getPlaylistDataRecursively = (url) => {
return fetch('/spotify/get-track-ids', {headers: {
'url': url
}})
.then(response => response.json())
.then(data => {
console.log(data)
setTitles(data.title)
setArtists(data.artist)
setFeatures(data.features)
setIds(data.track_ids)
if (data.next_url) {
const next_url = data.next_url.replace('https://api.spotify.com/v1', '')
return getPlaylistDataRecursively(next_url)
}
})
}
return getPlaylistDataRecursively(`/playlists/${playlistId}/tracks/?offset=0&limit=100`)
.then(() => hideLoading());
}
或者,使用异步/等待:
const fetchPlaylist = async (playlistId) => {
showLoading()
const getPlaylistDataRecursively = async (url) => {
const response = await fetch('/spotify/get-track-ids', {headers: {
'url': url
}})
const data = response.json()
console.log(data)
setTitles(data.title)
setArtists(data.artist)
setFeatures(data.features)
setIds(data.track_ids)
if (data.next_url) {
const next_url = data.next_url.replace('https://api.spotify.com/v1', '')
await getPlaylistDataRecursively(next_url)
}
}
await getPlaylistDataRecursively(`/playlists/${playlistId}/tracks/?offset=0&limit=100`)
hideLoading()
}
您始终需要返回
承诺
const fetchPlaylist = (playlistId) => {
showLoading()
const getPlaylistDataRecursively = (url) => {
return fetch('/spotify/get-track-ids', {headers: {
'url': url
}})
.then(response => response.json())
.then(data => {
console.log(data)
setTitles(data.title)
setArtists(data.artist)
setFeatures(data.features)
setIds(data.track_ids)
if (data.next_url) {
const next_url = data.next_url.replace('https://api.spotify.com/v1', '')
return getPlaylistDataRecursively(next_url)
}
})
}
return getPlaylistDataRecursively(`/playlists/${playlistId}/tracks/?offset=0&limit=100`)
.then(() => hideLoading());
}
或者,使用异步/等待:
const fetchPlaylist = async (playlistId) => {
showLoading()
const getPlaylistDataRecursively = async (url) => {
const response = await fetch('/spotify/get-track-ids', {headers: {
'url': url
}})
const data = response.json()
console.log(data)
setTitles(data.title)
setArtists(data.artist)
setFeatures(data.features)
setIds(data.track_ids)
if (data.next_url) {
const next_url = data.next_url.replace('https://api.spotify.com/v1', '')
await getPlaylistDataRecursively(next_url)
}
}
await getPlaylistDataRecursively(`/playlists/${playlistId}/tracks/?offset=0&limit=100`)
hideLoading()
}
还将添加等待showLoading()
,无论如何回答都很好谢谢你,Hagai。问题中没有任何内容表明showLoading()或hideLoading()返回了承诺。这些方法通常是同步的。但是是的,如果它们是异步的,那么应该等待它们。还将添加await showLoading()
,不管怎样,非常好的回答谢谢,Hagai。问题中没有任何内容表明showLoading()或hideLoading()返回了承诺。这些方法通常是同步的。但是是的,如果它们是异步的,那么应该等待它们。