Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa | |||
|
wiki2:js:notes [2022/11/03 08:33] alfred borrado |
— (actual) | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| - | ====== JavaScript Notes ====== | ||
| - | ===== Snippets ===== | ||
| - | |||
| - | ==== ES6 Events ==== | ||
| - | <code> | ||
| - | const EventEmitter = require('events'); | ||
| - | |||
| - | class Client extends EventEmitter | ||
| - | { | ||
| - | eventTest() { | ||
| - | this.emit('event'); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | let testClient = new Client(1,2,3,4,5); | ||
| - | testClient.once('event', () => {console.log('triggerd1!')} ); | ||
| - | testClient.on('event', () => {console.log('triggerd2!')} ); | ||
| - | testClient.eventTest(); | ||
| - | testClient.eventTest(); | ||
| - | </code> | ||
| - | |||
| - | ==== ES6 Events to Promises ==== | ||
| - | pues es tema de que se encolan los errores. Es decir, pongamos que tengo varias promises, una para (yo que sé) login, logout, cookies. Todas con su then y catch. De repente hay un error en logout, se lanzarán todos los errores (login, logout y cookies). Aún peor, si pongo un if en logout para recibir "error de logout" el evento se recibirá en todos, pero ya no se volverá a recibir en logout. La coña de las promises es que una vez se lancen no lo vuelven a hacer, he de hacer un control muy bestia de errores (no me vale pillar el evento con once). | ||
| - | <code> | ||
| - | const EventEmitter = require('events'); | ||
| - | |||
| - | class Client extends EventEmitter | ||
| - | { | ||
| - | constructor() { | ||
| - | super(); | ||
| - | this.num = 0; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | class Wrapper | ||
| - | { | ||
| - | constructor (tclient) { | ||
| - | this.client = tclient; | ||
| - | } | ||
| - | |||
| - | login() { | ||
| - | return new Promise(function (resolve, reject) { | ||
| - | this.client.on('login', () => { | ||
| - | console.log("received event login"); | ||
| - | resolve(); | ||
| - | }); | ||
| - | this.client.on('eerror', (error) => { | ||
| - | if (error === "login error") reject(error); | ||
| - | }); | ||
| - | }.bind(this)); | ||
| - | } | ||
| - | |||
| - | logout() { | ||
| - | return new Promise(function (resolve, reject) { | ||
| - | this.client.on('logout', () => { | ||
| - | console.log("received event logout"); | ||
| - | resolve(); | ||
| - | }); | ||
| - | this.client.on('eerror', (error) => { | ||
| - | if (error === "logout error") reject(error); | ||
| - | }); | ||
| - | }.bind(this)); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | let client = new Client(); | ||
| - | let wrapper = new Wrapper(client); | ||
| - | |||
| - | var p1 = wrapper.login().then(function() { | ||
| - | console.log("login"); | ||
| - | }).catch(function(error) { | ||
| - | console.error("Reception error login"); | ||
| - | }); | ||
| - | |||
| - | var p2 = wrapper.logout().then(function() { | ||
| - | console.log("logout"); | ||
| - | }).catch(function(error) { | ||
| - | console.error("Reception error logout"); | ||
| - | }); | ||
| - | |||
| - | |||
| - | setTimeout(function() { | ||
| - | client.emit('eerror', 'login error'); | ||
| - | client.emit('logout'); | ||
| - | client.emit('logout'); | ||
| - | client.emit('logout'); | ||
| - | client.emit('eerror', 'logout error'); | ||
| - | }, 1000 * 1); | ||
| - | </code> | ||
| - | ===== Resources ===== | ||
| - | |||
| - | <code> | ||
| - | > b = a.c || 4; | ||
| - | 4 | ||
| - | </code> | ||
| - | |||
| - | ===== JQuery (again) ===== | ||
| - | |||
| - | <code> | ||
| - | <a href="#" class="deleteUser" data-id="33" /> | ||
| - | |||
| - | // Now we can take it with JQuery like this: | ||
| - | $('.deleteUser').on('click', function() { | ||
| - | $(this).data('id') | ||
| - | }); | ||
| - | </code> | ||