Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // promise that defines object and passes it to next function
- function f1() {
- return new Promise((resolve, reject) => {
- var obj = {
- key1: "info",
- key2: "more info",
- etc: "and on and on..."
- };
- resolve(obj);
- });
- }
- // calls f1 and calls callback of f3 with new data from then of f1
- function f2(cb) {
- f1().then((obj) => {
- console.log(obj); // Outputs actual values in obj as defined in f1
- cb.call(obj); // Successfully calls function, but "this" is not replaced with "obj" in callback
- })
- }
- // overarching function that initiates chain of events
- function f3(cb) {
- f2(cb);
- };
- f3(() => {
- console.log(this); // Outputs an empty object
- });
Add Comment
Please, Sign In to add comment