SlideShare a Scribd company logo
1 of 14
AngularJS Promises
Present about AngularJS
Promises
Promises
❖Thế nào là promises?
❖Những tính năng, đặc điểm nào mà AngularJS promises có
❖Mục đích sử dụng AngularJS promises
❖Demo sử dụng promises cho việc load images
Code và Demo
❖Basic Promises
❖Multiple Promises
❖Image Preloading Service
AngularJS Promises
Làm cái kia trước rồi làm cái
này..
Thế nào là promise?
❖Là 1 phương thức dùng để quán lý các hành động không đồng bộ khác
nhau được thực hiện ở trong app.
❖Khi hành động nào đó được thực hiện xong, thì bạn có thể xý lý được kết
quả.
❖Bạn có hiểu là: Khi bạn làm xong việc này thì bạn có thể làm việc khác.
Promise = $.ajax?
❖Chúng ta đã quen với khái niệm của Ajax callback, đó là:
➢ $ajax(url).done(function(response) {});
❖$.ajax sẽ trả về môt jQuery promise, cái mà giống với promise nhưng nó
khác so với AngularJS promises.
❖Hệ thống AngularJS promise đã được mở rộng hơn so với promise jQuery
Ajax.
Tính năng đặc điểm của AngularJS Promise
❖ Nó không bị giới hạn chỉ để dùng gọi APIs.
❖ Bạn có thể sử dụng nó để quản lý các tiến trình của hệ thống logic trong code.
❖ Bạn có thể thực hiện chuỗi các hành động sau khi một promise hoàn thành. "Khi bạn thực hiện
xong cái kia thì làm cái này, cái đó ..."
❖ Bạn có thể xử lý một cách riêng biệt khi thành công, lỗi, tiến trình.
❖ Bạn có thể nhóm nhiều promises khác nhau vào chung một promise. "Khi bạn thực hiện xong
8 hành động này thì bạn thực hiện hành động kia"
❖ Khi bạn có một promise nhưng nó đã được xử lý và kết thúc thì bạn không phải chờ để thực
hiện hành động khác.
Promise System
❖Hệ thống Promise thì chạy trên AngularJS, nó sử dụng $q service để hoạt
động
❖Nó bao gồm:
- Deferred API
- Promise API
Deferred API
 Khởi tạo đối tượng deferred bằng function $q.defer()
 resolve(value)
 reject(reason)
 notify(value)
 thuộc tính promise
Promise API
 Đối tượng Promise là một thuộc tính của deferred
 then(successCallback, errorCallback, notifyCallback)
 catch(errorCallback)
 finally(callback)
Angular $q
 defer() – khởi tạo đối tượng deferred
 reject(reason) – giống như throw ở JS
 when(value) – bọc lấy đối tượng khác giống như một promise
 all(promiseArray) – khởi tạo một promise mới từ nhiều các promises khác
nhau
Simple Promise Demo
 Ở đây chúng ta sẽ sử dụng promise để preload một bức ảnh.
 Code ở đây:
- Khởi tạo đối tượng deferred
- Sử dụng 1 tiền trình dài, đó là load ảnh
- Lưu promise và thêm then() để điều khiển những gì tiếp theo khi load hoàn tất
 Plunker Simple Promise Demo
Promise Use Cases
 API calls
- Khi API call thành công và hoàn tất thì ta thu được data, từ đó sẽ sử
data đó trong hệ thống.
- Bạn có sử dụng $http? $http.get() sẽ trả về một promise, từ đó ta sử
sử dụng .then() của Promise API để sử lý tiến trình. $resource cũng
có cách hoạt động tương tự, nó cũng sử dụng $http.
Thanks

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

AngularJS Promises

  • 1. AngularJS Promises Present about AngularJS Promises
  • 2. Promises ❖Thế nào là promises? ❖Những tính năng, đặc điểm nào mà AngularJS promises có ❖Mục đích sử dụng AngularJS promises ❖Demo sử dụng promises cho việc load images
  • 3. Code và Demo ❖Basic Promises ❖Multiple Promises ❖Image Preloading Service
  • 4. AngularJS Promises Làm cái kia trước rồi làm cái này..
  • 5. Thế nào là promise? ❖Là 1 phương thức dùng để quán lý các hành động không đồng bộ khác nhau được thực hiện ở trong app. ❖Khi hành động nào đó được thực hiện xong, thì bạn có thể xý lý được kết quả. ❖Bạn có hiểu là: Khi bạn làm xong việc này thì bạn có thể làm việc khác.
  • 6. Promise = $.ajax? ❖Chúng ta đã quen với khái niệm của Ajax callback, đó là: ➢ $ajax(url).done(function(response) {}); ❖$.ajax sẽ trả về môt jQuery promise, cái mà giống với promise nhưng nó khác so với AngularJS promises. ❖Hệ thống AngularJS promise đã được mở rộng hơn so với promise jQuery Ajax.
  • 7. Tính năng đặc điểm của AngularJS Promise ❖ Nó không bị giới hạn chỉ để dùng gọi APIs. ❖ Bạn có thể sử dụng nó để quản lý các tiến trình của hệ thống logic trong code. ❖ Bạn có thể thực hiện chuỗi các hành động sau khi một promise hoàn thành. "Khi bạn thực hiện xong cái kia thì làm cái này, cái đó ..." ❖ Bạn có thể xử lý một cách riêng biệt khi thành công, lỗi, tiến trình. ❖ Bạn có thể nhóm nhiều promises khác nhau vào chung một promise. "Khi bạn thực hiện xong 8 hành động này thì bạn thực hiện hành động kia" ❖ Khi bạn có một promise nhưng nó đã được xử lý và kết thúc thì bạn không phải chờ để thực hiện hành động khác.
  • 8. Promise System ❖Hệ thống Promise thì chạy trên AngularJS, nó sử dụng $q service để hoạt động ❖Nó bao gồm: - Deferred API - Promise API
  • 9. Deferred API  Khởi tạo đối tượng deferred bằng function $q.defer()  resolve(value)  reject(reason)  notify(value)  thuộc tính promise
  • 10. Promise API  Đối tượng Promise là một thuộc tính của deferred  then(successCallback, errorCallback, notifyCallback)  catch(errorCallback)  finally(callback)
  • 11. Angular $q  defer() – khởi tạo đối tượng deferred  reject(reason) – giống như throw ở JS  when(value) – bọc lấy đối tượng khác giống như một promise  all(promiseArray) – khởi tạo một promise mới từ nhiều các promises khác nhau
  • 12. Simple Promise Demo  Ở đây chúng ta sẽ sử dụng promise để preload một bức ảnh.  Code ở đây: - Khởi tạo đối tượng deferred - Sử dụng 1 tiền trình dài, đó là load ảnh - Lưu promise và thêm then() để điều khiển những gì tiếp theo khi load hoàn tất  Plunker Simple Promise Demo
  • 13. Promise Use Cases  API calls - Khi API call thành công và hoàn tất thì ta thu được data, từ đó sẽ sử data đó trong hệ thống. - Bạn có sử dụng $http? $http.get() sẽ trả về một promise, từ đó ta sử sử dụng .then() của Promise API để sử lý tiến trình. $resource cũng có cách hoạt động tương tự, nó cũng sử dụng $http.