Guest User

Untitled

a guest
Jan 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. // promise that defines object and passes it to next function
  2. function f1() {
  3.  
  4. return new Promise((resolve, reject) => {
  5. var obj = {
  6. key1: "info",
  7. key2: "more info",
  8. etc: "and on and on..."
  9. };
  10. resolve(obj);
  11. });
  12.  
  13. }
  14.  
  15. // calls f1 and calls callback of f3 with new data from then of f1
  16. function f2(cb) {
  17.  
  18. f1().then((obj) => {
  19. console.log(obj); // Outputs actual values in obj as defined in f1
  20. cb.call(obj); // Successfully calls function, but "this" is not replaced with "obj" in callback
  21. })
  22.  
  23. }
  24.  
  25. // overarching function that initiates chain of events
  26. function f3(cb) {
  27. f2(cb);
  28. };
  29.  
  30. f3(() => {
  31. console.log(this); // Outputs an empty object
  32. });
Add Comment
Please, Sign In to add comment