Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. // Function with a promise
  2.  
  3. const doAsyncStuff = () => {
  4. return new Promise((resolve, reject) => {
  5. if (stuff) {
  6. resolve('cool');
  7. }
  8. reject('bad');
  9. });
  10. };
  11.  
  12. // Promise function resolution, old style
  13.  
  14. main() {
  15. doAsyncStuff().then(res => {
  16. // res === 'cool'
  17. }).catch(err => {
  18. // err === 'bad'
  19. });
  20. }
  21.  
  22.  
  23. // Promise function resolution, async/await style
  24.  
  25. async main() {
  26. try {
  27. const isCool = await asyncStuff();
  28. // isCool === 'cool'
  29. } catch (err) {
  30. // err === 'bad'
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement