简体   繁体   中英

Returning promises in javascript

I need some help returning a promise. I don't get why I am not getting the players value

The code

const playerSelectName = document.getElementById('sel1');
const playerSelectPosition = document.getElementById('sel2');
const playerSelectAge = document.getElementById('sel3');
const searchButton = document.getElementById('search-btn');

async function getPlayers() {
    const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty');
    const playersObject = await response.json();
    return playersObject;
}

searchButton.addEventListener('click', async () => {
    const players = await getPlayers();
    let [ name, position, age ] = [ playerSelectName.value, playerSelectPosition.value, playerSelectAge.value ];
    yersObject.filter((playerAge) => {});
});

I cant get to access the code inside my listener function of the value players

the problem was in destructuring check the below snippet. I hope this will solve the issue . Also please update what you are trying to achieve in filter so i can add on the solution.

I didnt understood what are you trying to do in destructure part and after that filter

 async function getPlayers() { const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty'); const playersObject = await response.json(); return playersObject; } const searchButton = document.getElementById('search-btn'); searchButton.addEventListener('click', async () => { const players = await getPlayers(); players.forEach(player => { const {name, nationality, position} = player console.log(name, nationality, position) }) }); 
 <button id='search-btn'> Load Results </button> 

Here's a simple example accessing the players to help get you started.

 async function getPlayers() { const response = await fetch('https://football-players-b31f2.firebaseio.com/players.json?print=pretty'); return response.json(); } (async () => { const players = await getPlayers(); console.log(players) })(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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