gesinn-it-pub / mwbot

A simple, but flexible MediaWiki bot for Node.js
MIT License
46 stars 15 forks source link

Add examples using async/await syntax #14

Closed lucaswerkmeister closed 6 years ago

lucaswerkmeister commented 6 years ago

Thanks for creating this library! I used it to write a command-line version of the namescript user script and found it quite pleasant to work with :)

One thing that I think could be helpful: examples on how to use the library with async/await syntax instead of chaining .then calls. Since Bluebird promises (or any other Promises/A+ promises) are fully compatible with ES6 promises, this works out quite well, and I found it to be a very pleasant style of programming:

async function main() {
    const bot = new MWBot({
        apiUrl: 'https://www.wikidata.org/w/api.php'
    });
    await bot.loginGetEditToken({
        username: …
        password: …
    });
    …
    for (const itemId of …) {
        await bot.request(…);
    }
}
main().catch(console.error);

Do you think it would be worth adding such examples? I could try to adapt some of the existing examples if you’re okay with it.

Fannon commented 6 years ago

Hello Lukas,

I'm glad this library works for you!

You are right. Since Node.js has proper async/await support, there's really no reason not to use the new syntax for it in almost all use cases.

I'll add your example to the documentation, thanks!

Fannon commented 6 years ago

https://github.com/Fannon/mwbot/commit/32dca91abec447f23d5596943e68c498a279d3e6#diff-04c6e90faac2675aa89e2176d2eec7d8

lucaswerkmeister commented 6 years ago

Cool, thank you!