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

10 mẹo sử dụng array và object trong javascript

10 mẹo sử dụng array và object trong javascript

jQuery on hover

Jquery on mouse over example.

Random String

Random String với độ dài tuỳ chọn

closure javascript

closure javascript

Kiểm tra định dạng email bằng Javascript

Kiểm tra định dạng email bằng Javascript