0

Why does my function not return the value it captures in an intermediate step?

I have a very simple function that calls another. The nested function returns a value with no issue. The outer function cannot capture this value. Why not?

here is the outer function

function getEmbeddableLink() {
  url =
    "https://graph.facebook.com/v8.0/p/giuhi/";

  axios.get(url, { headers: { Authorization: AuthStr } }).then(
    (response) => {
      var videoUrl = response.data.html.url;
      );
      let videoUrl2 = getVideo(videoUrl);
      console.log("outer response: "+videoUrl2);
      return getVideo(videoUrl);
    },
    (error) => {
      console.log("error: " + error);
    }
  );
}
function getVideo(videoUrl) {
  let url= videoUrl+"/?__a=1";
  axios
    .get(url, {
      headers: {
        "Content-Type": "application/json;",
        Authorization: AuthStr,
      },
    })
    .then(
      (response) => {
          let videoUrl2 = response.data.graphql.shortcode_media.video_url;
        console.log(
          "inner response: " + videoUrl2
        );
        return videoUrl2;
      },
      (error) => {
        console.log("error: " + error);
      }
    );
}

and my node.js application calls the outer function like this...

exports.findContent = (req, res) => {
  let videoUrl = getEmbeddableLink();
  res.send({"videoUrl": videoUrl});
};

in getEmbeddable link
videoUrl: undefined
in getVideo()
videoUrl2: undefined
response: https://scontent-lax3-1.cdninstagram.com/v/t50.2886-16/57618934_655274868266223_1409654894054808381_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5mZWVkLmRlZmF1bHQiLCJxZV9ncm91cHMiOiJbXCJpZ193ZWJfZGVsaXZlcnlfdnRzX290ZlwiXSJ9&_nc_ht=scontent-lax3-1.cdninstagram.com&_nc_cat=103&_nc_ohc=UI0_B6BObP4AX9kHL-4&vs=17869761679361470_2183106319&_nc_vs=HBksFQAYJEdQWXhid1B2MU1BUl9GTUNBRDBiQjFDb0daQVRia1lMQUFBRhUAAsgBABUAGCRHR1FxYndQOGlzNHdHb1FDQVBUa3k0UmpKM3AtYmtZTEFBQUYVAgLIAQAoABgAGwAVAAAYABbkz%2Fzc4JC4PxUCKAJDMywXQCczMzMzMzMYEmRhc2hfYmFzZWxpbmVfMV92MREAdeoHAA%3D%3D&oe=5F3A7A16&oh=f7a6d063f37df08b23ff958a0f0ce845

I presume my problem is related to the nature of asynchronous calls. If so, please explain what changes I must make. Cheers.

GNG
  • 1,341
  • 2
  • 23
  • 50
  • Because the nested operations are non-blocking. They start their operation and then immediately return BEFORE they complete. They complete and call their callback sometime later. This is typical of asynchronous programming in nodejs and something one has to learn in order to program with asynchronous operations in Javascript. – jfriend00 Aug 16 '20 at 00:21
  • That `return getVideo(videoUrl);` is inside a callback function, it does not `return` from `getEmbeddableLink` – Bergi Aug 16 '20 at 00:25
  • Also, note that your `getVideo()` function does not return anything. Your `return` statement is inside another callback and thus only returns from that callback, does not return from the function itself. – jfriend00 Aug 16 '20 at 00:26
  • You'll want to put `return` statements in both of the functions to return the promise chains that you are constructing. – Bergi Aug 16 '20 at 00:26

0 Answers0