What is the output when running the following Node.js code? const events = require('events'); const emitter =
4 phương ánCó giải thíchDễ
new events.EventEmitter();
emitter.on('event', () => console.log('Event triggered'));
emitter.emit('event');
console.log('End');Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Khi chạy mã Node.js này, chúng ta sẽ thấy hai dòng được in ra console. Đầu tiên là 'Event triggered' do sự kiện được kích hoạt và sau đó là 'End'. Điều này xảy ra vì chúng ta đã sử dụng phương thức emitter.on để lắng nghe sự kiện 'event' và phương thức emitter.emit để kích hoạt sự kiện này. Khi sự kiện được kích hoạt, hàm console.log('Event triggered') được thực thi và in ra 'Event triggered' vào console. Sau đó, chương trình tiếp tục thực thi dòng console.log('End') và in ra 'End'. Vì vậy, kết quả cuối cùng khi chạy mã này sẽ là 'Event triggered End'.
Cùng chương 1
Which of the following are valid methods to read a file in Node.js? (Choose 2 answers)Có lời giảiCâu hỏiHow to handle asynchronous operations in a more readable and maintainable way? (Choose 2 answers)Có lời giảiCâu hỏiWhich statements about Promises in Node.js are true? (Choose 2 answers)Có lời giảiCâu hỏiCả 18 câu của chủ đề18 câu