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 )
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…