[PROMISE] Tìm hiểu về promise trong ES6
https://viblo.asia/p/tim-hieu-ve-promise-trong-es6-DXOkRjPPkdZ
Ví dụ 1: Giả sử ta có một tác vụ muốn delay 5s rồi in ra chữ “LẬP” tiếp đó 3s in ra chú “TRÌNH” sau cùng 2s in ra chữ “JAVASCRIPT”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
setTimeout(function(){
console.log('LẬP');
setTimeout(function(){
console.log('TRÌNH');
setTimeout(function(){
console.log('JAVASCRIPT');
},2000);
},3000);
}, 5000);
function handleTimeout(timeout) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, timeout);
});
}
var xxx = handleTimeout(5000);
xxx.then(function () {
console.log('LẬP');
})
.then(function () {
return handleTimeout(3000);
})
.then(function () {
console.log('TRÌNH')
})
.then(function () {
return handleTimeout(1000);
})
.then(function () {
console.log('JAVASCRIPT');
});
</script>
</body>
</html>ví dụ 2: Một trường hợp khác, nếu bạn có task là tổng hợp một thông tin lấy nhiều nguồn từ nhiều API. Không cách nào dễ dàng và tốt khi sử dụng Promise.all trong trường hợp này.
Promise.all trong trường hợp này. 
Tìm hiểu về promise trong ES6
1. Promise là gì ?
2. Các trạng thái của promise
3. Cách tạo 1 promise
5. Catch trong promise
6. Một vài ví dụ ứng dụng promise
Last updated