Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. /**
  2. * Tạo 1 hàm doAfter nhận vào 2 tham số:
  3. * - Tham số thứ 1: 1 function
  4. * - Tham số thứ 2: Thời gian x (ms)
  5. * Hàm này sẽ gọi function sau 1 khoảng thời gian x ms VÀ trả về 1 promise để có thể gọi như sau
  6. */
  7. function doAfter(func,times) {
  8. return new Promise(function(resolve, reject){
  9. setTimeout(function(){
  10. resolve(func());
  11. },times);
  12. });
  13.  
  14. };
  15.  
  16. function sayHello() {
  17. console.log('Hello');
  18. }
  19.  
  20. function sayGoodbye() {
  21. console.log('Goodbye');
  22. }
  23.  
  24. doAfter(sayHello, 1000).then(sayGoodbye);
  25. // Expect:
  26. // Đợi 1s
  27. // Hello
  28. // Goodbye
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement