【问题标题】:What is the correct way to get data from async function?从异步函数获取数据的正确方法是什么?
【发布时间】:2025-11-22 13:50:01
【问题描述】:

在这里,我实现了一个名为 fetchDetails 的函数,我用它来使用回调获取数据。 根据我的说法,调用函数/它的回调的语法不正确。 它每次都会触发“回调不是函数”的错误。 如何调用回调以从 fetchDetails 获取该数据。

已经尝试返回数据而不是回调(数据);

            var flag = false;
                    function fetchDetail(serise, callback) {
                        if(responseData.body.data.length - 1 !== serise){
                            // process to fetch data from array of responsedata 
                        }
            else
                        {
            // Here if the serise is reached at length of the array then will true flag
                            flag = true;
                        }

                        if(flag){
                //if the flag is true then, wanted to get data which is callback by function fetchDetails()
                            callback(); // Here my callback get stuck and get error "Callback is not a function"
                return;
                        }
                        else
            {
                // Here increase serise for getting next element of an array
                            fetchMessage(++serise);
                        }
                   }
           fetchDetail(0,function(){
                      let data ={
                         isFound: 1,
                         detail: "detail found"
                      }
              callback(data);                
                   });

预期结果是获取fetchDetail()中定义的数据,但进程卡在

if (flag) { 
 callback();
}

【问题讨论】:

  • 为什么不flag = serise === (responseData.body.data.length - 1); if (!flag) { /* process to fetch data from array of responsedata */ }
  • 您在callback(data) 中收到错误消息。因为那里callback 没有任何意义。
  • @mplungjan 我需要关于回调的解决方案。
  • @deceze 我是 nodejs 和 javascript 的新手。我如何得到回应。

标签: javascript node.js asynchronous callback


【解决方案1】:

错误不在块中

if (flag) { 
   callback();
}

错误在块中

fetchDetail(0,function(){
      let data ={
      isFound: 1,
      detail: "detail found"
   }
   callback(data);                
});

因为这个块中的回调没有定义。

你可以像这样修改你的代码:

var flag = false;

function fetchDetail(serise, callback) {
  if (responseData.body.data.length - 1 !== serise) {
    // process to fetch data from array of responsedata 
  }
  else {
    // Here if the serise is reached at length of the array then will true flag
    flag = true;
  }
  if (flag == 0) {
    //if the flag is true then, wanted to get data which is callback by function fetchDetails()
    data = callback(); // Here my callback get stuck and get error "Callback is not a function"
    console.log(data)
    return;
  } else {
    // Here increase serise for getting next element of an array
    console.log('Not call callback')
  }
}
fetchDetail(0, function() {
  let data = {
    isFound: 1,
    detail: "detail found"
  }
  return data
  // console.log('Hello from callback')
});

【讨论】:

    【解决方案2】:

    在您的函数 fetchDetails 中,您传递了一个函数作为第二个参数。 所以在 fetchDetails 里面,'callback' 是

    function() {
       let data = {
          isFound: 1,
          detail: "detail found"
        }
        callback(data);
     }  
    

    它本身正在调用一个叫做回调的东西(这是未定义的)。

    我不完全确定您的代码在做什么,但是通过回调的示例可能是这样的:

     let fetchDetails = function(callback) {
        fetch(url).then(function(data) {
            callback.call(this, data);
        });
     }
    
     fetchDetails(function(data) {
         console.log(data);
     });
    

    }

    【讨论】:

      【解决方案3】:

      如今,将 async/await 语法用于异步函数变得更加容易:

      // you can use await keyword only in async functions.
      async function init(){  
          var value=await fetchDetails()  // See the await keyword
          console.log(value)              // hello
      }
      
      async function fetchDetail(serise) { // No callback parameter.
      
           var callback
           var promise=new Promise(resolve=>callback=resolve)
      
           // do your async work, call callback when you finish
      
           setTimeout(()=>{callback('hello')},1000)
           return promise
      }
      

      【讨论】: