2015-09-21 102 views
0

即时通讯完全是新的node.js,我找不到类似的问题,我的问题。我确信很容易解决你们中的一个......至少我猜。Node.js模块功能的访问参数

我想使用node.js的npm mediawiki模块来获取wikipage的特殊段落!我得到的段落使用预定义的功能如下:

bot.page(title).complete(function (title, text, date) { 
    //extract section '== Check ==' from wikipage&clean string 
    var result = S(text).between('== Check ==', '==').s; 
}); 

这就是工作。我想要的是:在其他函数中使用该代码块之外的“结果”。我认为它与回调有关,但我不知道如何处理它,因为这是来自mediawiki模块的预定义函数。

模块得到一个Wiki页面的例子函数看起来如下:

/** 
    * Request the content of page by title 
    * @param title the title of the page 
    * @param isPriority (optional) should the request be added to the top of the request queue (defualt: false) 
    */ 
    Bot.prototype.page = function (title, isPriority) { 
     return _page.call(this, { titles: title }, isPriority); 
}; 

它采用模块的以下功能:

function _page(query, isPriority) { 
    var promise = new Promise(); 

    query.action = "query"; 
    query.prop = "revisions"; 
    query.rvprop = "timestamp|content"; 

    this.get(query, isPriority).complete(function (data) { 
     var pages = Object.getOwnPropertyNames(data.query.pages); 
     var _this = this; 
     pages.forEach(function (id) { 
      var page = data.query.pages[id]; 
      promise._onComplete.call(_this, page.title, page.revisions[0]["*"], new Date(page.revisions[0].timestamp)); 
     }); 
    }).error(function (err) { 
     promise._onError.call(this, err); 
    }); 

    return promise; 
} 

还有一个完整的回调函数,我不知道如何使用它:

/** 
    * Sets the complete callback 
    * @param callback a Function to call on complete 
    */ 
    Promise.prototype.complete = function(callback){ 
     this._onComplete = callback; 
     return this; 
    }; 

如何访问“结果”变量通过在模块的功能之外使用回调?我不知道如何处理回调,因为它是一个模块的预定义功能...

+0

可能重复[如何返回从异步调用的响应?](http://stackoverflow.com/问题/ 14220321 /如何返回来自异步调用的响应) –

回答

0

我想要的是:在其他功能的代码块外使用“结果”。

你不行。您需要使用该代码块内的结果(该代码块被称为callback函数btw。)。您仍然可以将它们传递到其他功能,您需要做的仅仅是它的回调函数里面:

bot.page(title).complete(function (title, text, date) { 
    //extract section '== Check ==' from wikipage&clean string 
    var result = S(text).between('== Check ==', '==').s; 

    other_function(result); // <------------- this is how you use it 
}); 
+0

好的,我明白了!感谢您的快速帮助! – dnks23