variable scope in asynchronous function

后端 未结 2 1881
[愿得一人]
[愿得一人] 2021-01-21 01:15

I\'ve built the function to return some variable. But my function uses another function asynchronously.

function getVariable() {
  var myVariable;
  asyncronousF         


        
2条回答
  •  感情败类
    2021-01-21 01:36

    They are the same variable, but you cannot return the result of an asynchronous function call synchronously from your getVariable function. The value of myVariable will be updated asynchronously at some unspecified later point in time. But your function is returning the value now. That doesn't work.

    That means your getVariable function is asynchronous as well, which means you have to design it that way. For example it could accept a callback, just like asyncronousFunction does.

提交回复
热议问题