等到eventlistener被要求返回承诺
我正在尝试返回获取调用的结果,但我想等到我的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);
});
(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.
<!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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其分为三个步骤。
id
处理用户选择Break this into three steps.
id