从异步功能获取数据的正确方法是什么?

时间:2019-07-17 08:00:48

标签: javascript node.js asynchronous callback

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

已经尝试返回数据,而不是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){
                //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();
}

3 个答案:

答案 0 :(得分:0)

在函数fetchDetails中,您要传递一个函数作为第二个参数。 因此在fetchDetails内部,“回调”为

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);
 });

}

答案 1 :(得分:0)

错误不在代码段中

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 :(得分:0)

今天,对异步功能使用异步/等待语法更加容易:

// 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
}