Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Problem with data in Mediawiki's api (probably in js syntax)

I am using snippet from this page marked as MediaWiki JS in my script:

 const params = {
        action: "parse",
        page: stronaDyskusji,
        prop: "wikitext",
        section: index_sekcji,
    };
    const api = new mw.Api();

    api.get(params).done((data) => {
       console.log(data.parse.wikitext["*"]);
    });
question from:https://stackoverflow.com/questions/65907715/problem-with-data-in-mediawikis-api-probably-in-js-syntax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

api.get().done() is probably asynchronous

Means it takes either a callback function or it returns a promise that needs to be resolved. In your case the function takes a callback function which gets called once api.get().done() has finished processing. Callbacks are often used to continue code execution after an asynchronous operation has completed (like in this case).

First example

api.get(params).done((data) => {
  console.log(data.parse.wikitext["*"]);
});

In your first code example, you are logging inside the callback function, means it will log after api.get().done() has finished.

Second example

var zwrot;
api.get(params).done((data) => {
  zwrot = data.parse.wikitext["*"];
});
console.log(zwrot);

In your second example you are trying to log the value of zwart before the callback function has been triggered. Therefore, no value has been assigned to zwart yet. So it logs undefined

( Read more about asynchronous )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...