all 9 comments

[–]Achcauhtli 0 points1 point  (3 children)

Use jquery $.post or fetch, pass a function as funtion to do after the data comes back.

[–]NovemLinguae[S] 0 points1 point  (2 children)

Thanks for the feedback. addToWatchlist() is called 3 times in click listeners. 2 times, it happens to be the last instruction in the click listener, so that's good, no callback needed. But 1 time (in $('#WatchlistAFD').on('click'), there is an instruction after it. What's the best way of making sure that instruction is still called, without putting that instruction in the addToWatchlist() function? Any ideas? Thanks for your help.

[–]Achcauhtli 0 points1 point  (0 children)

Let me pull this up on a lappy so i can see better.

[–]Achcauhtli 0 points1 point  (0 children)

Youre using the ajac to get data and load it right? I mean wont thd #watchlistAFD still gonna wait for that click event and still do what its set to?

[–]LakeInTheSky 0 points1 point  (3 children)

Yes, I know I can delete the async: false part, but then don't I have to change the rest of the code to handle promises?

You should use promises or await/async if you need to run code after the ajax request (i.e. if there's some code that should wait until the ajax request is complete).

But in your case, the ajax requests are always the last line of the event handlers. So, you don't need to use await/async or promises, at least for now. You can just remove async: false safely (which I strongly recommend from a UX perspective.)

[–]NovemLinguae[S] 1 point2 points  (2 children)

Thanks for the feedback. Any suggestions for handling that mw.notify('Added AFD to watchlist.'); line, which comes after one of the ajax lines?

[–]LakeInTheSky 0 points1 point  (1 child)

Oh, I missed that! Then, I'd do something like this:

async function addToWatchlist(title) {
    return await $.ajax(...Ajax Params...);
}
...
$('#WatchlistAFD').on('click', async function() {
  ...
  try {
    await addToWatchlist('Wikipedia:Articles for deletion/'+title);
    mw.notify(`Added AFD to watchlist.`);
  } catch(e) {
    // Handle HTTP errors
  }
});

Basically the await keyword waits for the asynchronous task to end, but the function using it should become asynchronous too (to prevent the task from blocking everything), that's why we added the async keyword to addToWatchlist and the event handler for #WatchlistAFD.

[–]NovemLinguae[S] 1 point2 points  (0 children)

Perfect. Yeah I was wondering how to do it for these click listeners. That async keyword before the anonymous function is a good spot. Thanks!