search

Home  >  Q&A  >  body text

Web crawler - node.js crawler External call function cannot obtain variable value

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!

習慣沉默習慣沉默2834 days ago569

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味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);
    }
    

    reply
    0
  • 黄舟

    黄舟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

    reply
    0
  • Cancelreply