等到eventlistener被要求返回承诺

发布于 2025-02-11 11:58:36 字数 5453 浏览 1 评论 0原文

我正在尝试返回获取调用的结果,但我想等到我的EventListener检测到动态渲染的选择器列表中的更改。

您会看到下面写的呼叫,从API中获取值并呈现值列表。我想知道是否有一种方法可以防止。我最终希望获得价值的结果。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dynamic List Test</title>
</head>
<body>
  <div class="container">
    <!-- App entry point -->
  </div>
  <script type="text/javascript" src="app.js"></script>
</body>
</html>
const testUrl =
  'https://hub.dummyapis.com/employee?noofRecords=10&idStarts=1001';

const result = fetch(testUrl, {
  method: 'GET',
})
  .then(resp => resp.json())
  .then(data => {
    // Create select list
    const mainEl = document.querySelector('.container');
    let selectList = document.createElement('select');
    selectList.id = 'selectorList';
    mainEl.appendChild(selectList);
    // // Prepare name list
    let nameArray = [];
    const nameList = data;
    nameArray.push(nameList);
    console.log(nameArray[0]);
    
    for (let last in nameArray[0]) {
      let surname = nameArray[0][last].lastName;
      let option = document.createElement('option');
      option.value = surname;
      option.text = surname;
      selectList.appendChild(option);
    }

    let selectorChoice = '';
    // Grab dynamic selector value
    selectList.addEventListener('change', e => {
      selectorChoice = e.target.value;
      console.log(selectorChoice);
      return selectorChoice;
    });
  })
  .then(resp => resp.json()) // I want to wait for the eventListener to be called before this happens
  .catch(error => console.log('Error:', error));

result.then(resp => {
  console.log(resp);
});

链接到codepen

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]0: {id: 1001, imageUrl: 'https://hub.dummyapis.com/Image?text=CA&height=120&width=120', firstName: 'Colt', lastName: 'Altenwerth', email: '[email protected]', …}1: {id: 1002, imageUrl: 'https://hub.dummyapis.com/Image?text=HW&height=120&width=120', firstName: 'Helena', lastName: 'Ward', email: '[email protected]', …}2: {id: 1003, imageUrl: 'https://hub.dummyapis.com/Image?text=LC&height=120&width=120', firstName: 'Liliana', lastName: 'Connelly', email: '[email protected]', …}3: {id: 1004, imageUrl: 'https://hub.dummyapis.com/Image?text=MH&height=120&width=120', firstName: 'Myah', lastName: 'Hane', email: '[email protected]', …}4: {id: 1005, imageUrl: 'https://hub.dummyapis.com/Image?text=MS&height=120&width=120', firstName: 'Mauricio', lastName: 'Stracke', email: '[email protected]', …}5: {id: 1006, imageUrl: 'https://hub.dummyapis.com/Image?text=CS&height=120&width=120', firstName: 'Candice', lastName: 'Sipes', email: '[email protected]', …}6: {id: 1007, imageUrl: 'https://hub.dummyapis.com/Image?text=EA&height=120&width=120', firstName: 'Evangeline', lastName: 'Aufderhar', email: '[email protected]', …}7: {id: 1008, imageUrl: 'https://hub.dummyapis.com/Image?text=RR&height=120&width=120', firstName: 'Rosetta', lastName: 'Rodriguez', email: '[email protected]', …}8: {id: 1009, imageUrl: 'https://hub.dummyapis.com/Image?text=TD&height=120&width=120', firstName: 'Tina', lastName: "D'Amore", email: "Tina.D'[email protected]", …}9: {id: 1010, imageUrl: 'https://hub.dummyapis.com/Image?text=PS&height=120&width=120', firstName: 'Pearline', lastName: 'Sawayn', email: '[email protected]', …}length: 10[[Prototype]]: Array(0)
app.js:37 Error: TypeError: Cannot read properties of undefined (reading 'json')
    at app.js:36:22
app.js:40 undefined
app.js:32 Hane

I'm trying to return the result of a fetch call but I want to wait until my eventListener detects a change within a dynamically rendered selector list.

You'll see the fetch call written below grabbing the values from an API and rendering the list of values. I was wondering if there is a way to prevent the .then(resp => resp.json()) from happening until a selection is made for selectList listener. I would ultimately like the get the value to result.then promise so that I can use that in another fetch call in the future.

Link to codepen

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dynamic List Test</title>
</head>
<body>
  <div class="container">
    <!-- App entry point -->
  </div>
  <script type="text/javascript" src="app.js"></script>
</body>
</html>
const testUrl =
  'https://hub.dummyapis.com/employee?noofRecords=10&idStarts=1001';

const result = fetch(testUrl, {
  method: 'GET',
})
  .then(resp => resp.json())
  .then(data => {
    // Create select list
    const mainEl = document.querySelector('.container');
    let selectList = document.createElement('select');
    selectList.id = 'selectorList';
    mainEl.appendChild(selectList);
    // // Prepare name list
    let nameArray = [];
    const nameList = data;
    nameArray.push(nameList);
    console.log(nameArray[0]);
    
    for (let last in nameArray[0]) {
      let surname = nameArray[0][last].lastName;
      let option = document.createElement('option');
      option.value = surname;
      option.text = surname;
      selectList.appendChild(option);
    }

    let selectorChoice = '';
    // Grab dynamic selector value
    selectList.addEventListener('change', e => {
      selectorChoice = e.target.value;
      console.log(selectorChoice);
      return selectorChoice;
    });
  })
  .then(resp => resp.json()) // I want to wait for the eventListener to be called before this happens
  .catch(error => console.log('Error:', error));

result.then(resp => {
  console.log(resp);
});

Result in the console

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]0: {id: 1001, imageUrl: 'https://hub.dummyapis.com/Image?text=CA&height=120&width=120', firstName: 'Colt', lastName: 'Altenwerth', email: '[email protected]', …}1: {id: 1002, imageUrl: 'https://hub.dummyapis.com/Image?text=HW&height=120&width=120', firstName: 'Helena', lastName: 'Ward', email: '[email protected]', …}2: {id: 1003, imageUrl: 'https://hub.dummyapis.com/Image?text=LC&height=120&width=120', firstName: 'Liliana', lastName: 'Connelly', email: '[email protected]', …}3: {id: 1004, imageUrl: 'https://hub.dummyapis.com/Image?text=MH&height=120&width=120', firstName: 'Myah', lastName: 'Hane', email: '[email protected]', …}4: {id: 1005, imageUrl: 'https://hub.dummyapis.com/Image?text=MS&height=120&width=120', firstName: 'Mauricio', lastName: 'Stracke', email: '[email protected]', …}5: {id: 1006, imageUrl: 'https://hub.dummyapis.com/Image?text=CS&height=120&width=120', firstName: 'Candice', lastName: 'Sipes', email: '[email protected]', …}6: {id: 1007, imageUrl: 'https://hub.dummyapis.com/Image?text=EA&height=120&width=120', firstName: 'Evangeline', lastName: 'Aufderhar', email: '[email protected]', …}7: {id: 1008, imageUrl: 'https://hub.dummyapis.com/Image?text=RR&height=120&width=120', firstName: 'Rosetta', lastName: 'Rodriguez', email: '[email protected]', …}8: {id: 1009, imageUrl: 'https://hub.dummyapis.com/Image?text=TD&height=120&width=120', firstName: 'Tina', lastName: "D'Amore", email: "Tina.D'[email protected]", …}9: {id: 1010, imageUrl: 'https://hub.dummyapis.com/Image?text=PS&height=120&width=120', firstName: 'Pearline', lastName: 'Sawayn', email: '[email protected]', …}length: 10[[Prototype]]: Array(0)
app.js:37 Error: TypeError: Cannot read properties of undefined (reading 'json')
    at app.js:36:22
app.js:40 undefined
app.js:32 Hane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-02-18 11:58:36

将其分为三个步骤。

  1. 准备:获取数据 +缓存数据
const fetchResult = fetch(...).then(response => response.json());
  1. 构建列表(使用ID来识别记录!)

fetchResult.then(data => {
  // Create select list
    const mainEl = document.querySelector('.container');
    let selectList = document.createElement('select');
    selectList.id = 'selectorList';
    mainEl.appendChild(selectList);

    data.forEach(employee => {
      const option = document.createElement('option');
      option.value = employee.id;
      option.text = employee.lastName;
      selectList.appendChild(option);
    });

    let selectList.addEventListener('change', e => {
      selectorChoice = parseInt(e.target.value);
      handleUserChoice(data, selectorChoice)
    });
  }
)

  1. 根据id处理用户选择
function handleUserChoice(data, id) {
    const chosenEmployee = data.find(e => e.id === id);
    
    if (choosenEmployee) {
      // ... do what you want here!
    }
}

Break this into three steps.

  1. Prepare: Fetch data + cache data
const fetchResult = fetch(...).then(response => response.json());
  1. Build list (use id to identify records!)

fetchResult.then(data => {
  // Create select list
    const mainEl = document.querySelector('.container');
    let selectList = document.createElement('select');
    selectList.id = 'selectorList';
    mainEl.appendChild(selectList);

    data.forEach(employee => {
      const option = document.createElement('option');
      option.value = employee.id;
      option.text = employee.lastName;
      selectList.appendChild(option);
    });

    let selectList.addEventListener('change', e => {
      selectorChoice = parseInt(e.target.value);
      handleUserChoice(data, selectorChoice)
    });
  }
)

  1. Handle user choice based on id
function handleUserChoice(data, id) {
    const chosenEmployee = data.find(e => e.id === id);
    
    if (choosenEmployee) {
      // ... do what you want here!
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文