Recently I wanted to write a crawler novel to read, but I encountered some difficulties
console.log(catalogUrl) can print out the required URL
But what I expect is that when I call the function var xxx = getCatalogUrl(book) externally, I can get the value of the catalogUrl variable.
I tried return and global variables, but none of them work.
Help all the experts!
淡淡烟草味2017-05-16 13:27:17
You need to understand js callback promise async/await
//简单的回调例子
asyncFunction(callback);
function callback(data){
console.log(data)
}
function asyncFunction(cb){
setTimeout(function(){
cb && cb(1); //传递一个1出去
},300);
}
黄舟2017-05-16 13:27:17
catalogUrl is in the asynchronous function! How to solve the asynchronous problem, look at these materials or Baidu, they are all basic things