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

Async await in javascript

Async await in javascript

Spread Operator for Objects

Using the spread operator during an Object declaration will assign the properties of the referenced Object to the new Object.

kiểm tra email trong javascript

kiểm tra email trong javascript

Lấy ngày hiện tại trong javascript

Lấy ngày hiện tại trong javascript

axios post await

Axios là một HTTP client được viết dựa trên Promises được dùng để hỗ trợ cho việc xây dựng các ứng dụng API từ đơn giản đến phức tạp. Có thể sử axios để post, get, put, delete...