简体   繁体   English

为什么在ES6中使用Promise时,事件监听器在一个事件之后不会持续?

[英]Why my event listener won't last after one event when using Promise in ES6?

I have following code (a simple one): 我有以下代码(一个简单的代码):

Code: 码:

 var myPromise = new Promise(function(success, error) { document.getElementById("success").addEventListener("click", function() { success({ msg: "This is success", code: 100 }); }); document.getElementById("error").addEventListener("click", function() { error({ msg: "This is error", code: 10 }); }); }); console.log("Hello"); myPromise.then(function(content) { console.log(content.msg); }); myPromise.catch(function(content) { console.log(content.code); }); 
 <button id="success">Success</button> <button id="error">Error</button> 

Clicking on either two buttons for first time will either log This is success or 10 . 首次单击两个按钮之一记录“ This is success或“ 10

Why clicking on either two button for second, third, and subsequent times will not log anything in console window? 为什么第二次,第三次及以后两次单击两个按钮都不会在控制台窗口中记录任何内容 why the event listener seems to be suddenly gone? 为什么事件监听器似乎突然消失了?

FYI: I'm currently experimenting with Promise in ES6 to understand how it works. 仅供参考:我目前正在ES6中尝试Promise,以了解其工作原理。

Thank You. 谢谢。

The EventListener is present only the Error/Success function won't be called again. 仅提供了Error / Success函数,不会再调用EventListener。 I added an console.info - Statement to show that the EventListener are being called. 我添加了console.info语句以显示正在调用EventListener。

 var myPromise = new Promise(function(success, error) { document.getElementById("success").addEventListener("click", function() { console.info("called success" ); success({ msg: "This is success", code: 100 }); }); document.getElementById("error").addEventListener("click", function() { console.info("called error" ); error({ msg: "This is error", code: 10 }); }); }); console.log("Hello"); myPromise.then(function(content) { console.log(content.msg); }); myPromise.catch(function(content) { console.log(content.code); }); 
 <button id="success">Success</button> <button id="error">Error</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法在使用淘汰赛js时向数组中的每个元素添加事件侦听器,而不仅仅是最后一个元素 - Why can't I add an event listener to each of my elements in my array, not just the last one when using knockout js ES6中的Polymer事件侦听器 - Polymer event listener in ES6 在ES6中删除事件侦听器 - Remove event listener in ES6 为什么在 jQuery 单击事件触发我的 es6 类函数之一后,我的其他类函数和变量变得未定义 - Why do my other class functions and variables become undefined after a jQuery click event triggers one of my es6 class functions 为什么事件监听器在点击时不显示模式? - Why won't my event listener display modal on click? 使用ES6箭头功能设置事件监听器 - Set event listener with ES6 arrow functions 在 es6 中,将带有回调的事件侦听器变成可迭代的 - in es6, make an event listener with a callback into an iterable 使用 es6 模块创建自定义“事件监听器” - Creating a custom “event listener” with es6 modules 为什么这个事件监听器不起作用? - Why won't this event listener work? 使用 ES6 而不重新刷新页面时,事件侦听器不会重新附加到我的 HTML 元素 - Event listeners don't get re-attached to my HTML elements when using ES6 without re-feshing the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM