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

Using promises to catch errors

Convert Array-like to True Array

Convert Array-like to True Array

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...

using javascript callback style error handling