[英]Access parameter of Node.js module function
我對node.js完全陌生,我找不到與我的問題類似的問題。 我敢肯定,解決你們中的一個很容易...至少我猜。
我正在嘗試使用用於node.js的npm mediawiki模塊獲取維基頁面的特殊段落! 我使用以下預定義函數獲取該段落:
bot.page(title).complete(function (title, text, date) {
//extract section '== Check ==' from wikipage&clean string
var result = S(text).between('== Check ==', '==').s;
});
就是這樣。 我想要的是:在其他功能的代碼塊之外使用“結果”。 我認為它與回調有關,但是我不確定如何處理它,因為這是來自mediawiki模塊的預定義函數。
獲取維基頁面的模塊的示例功能如下:
/**
* 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;
};
如何通過使用模塊功能之外的回調來訪問“結果”變量? 我不知道如何處理回調,因為它是模塊的預定義功能...
我想要的是:在其他功能的代碼塊之外使用“結果”。
你不能 您需要在該代碼塊內部使用該結果(該代碼塊稱為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
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.