Promise + gì

promise,es6,async,await,tipjs

Code viết tay: Promise +

const PENDING = 'PENDING';
const RESOLVED = 'RESOLVED';
const REJECTED = 'REJECTED';

class Promise {
	constructor(executor) {
		this.status = PENDING; 
		this.value = undefined; 
		this.reason = undefined; 
		let resolve = (value) => {
			if (this.status === PENDING) {
				this.value = value;
				this.status = RESOLVED;
			}
		};
		let reject = (reason) => {
			if (this.status === PENDING) {
				this.reason = reason;
				this.status = REJECTED;
			}
		};
		
		try {
			executor(resolve, reject);
		} catch(e) {
			console.log('catch::::', e);
			reject(e); 
		}
	}
	then(onfulfilled, onrejected) {
		if (this.status === RESOLVED) {
			onfulfilled(this.value);
		}
		if (this.status === REJECTED) {
			onrejected(this.reason);
		}
	}
}
module.exports = Promise;

Có thể bạn đã miss một số snippets code

Callback in javascript

Callback in javascript

Extend Object javascript

Extend Object javascript

Filter Unique Values

Kiểu đối tượng Set đã được giới thiệu trong ES6 và cùng với ..., ‘spread’ operator, chúng ta có thể sử dụng nó để tạo một mảng mới chỉ với các giá trị duy nhất.

kiểm tra email trong javascript

kiểm tra email trong javascript

jQuery on hover

Jquery on mouse over example.