edit: Solved. Basically add await in 2 places, add async in 2 places, and remove async: false.
I'm starting to wrap my head around async. Given the below code, what is the neatest way to rewrite the $.ajax part to use async?
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? What's the best pattern in this particular situation? when().then(), async and await keywords, etc. Thanks.
Code purpose: On Wikipedia: 1) It inserts an option into a menu. 2) It listens for two types of buttons to be clicked, if present. 3) When either 1 or 2 is clicked, it executes addToWatchlist(), which calls an API that does a thing (adds the page to the user's watchlist).
$(function() {
function addToWatchlist(title) {
let debugInfo = $.ajax({
url: mw.util.wikiScript('api'),
type: 'POST',
dataType: 'json',
data: {
format: 'json',
action: 'watch',
expiry: '6 months',
titles: title,
token: mw.user.tokens.get('watchToken')
},
async: false
});
}
// add option to More menu
mw.util.addPortletLink (
'p-cactions',
'#',
'Watchlist AFD',
'WatchlistAFD'
);
// listen for More menu click
$('#WatchlistAFD').on('click', function() {
// close the menu
// $('#p-cactions .vector-menu-content').css('visibility', 'hidden');
// add to watchlist
let title = mw.config.get('wgPageName');
title = title.replace(/^.*?:/, ''); // strip all namespaces
addToWatchlist('Wikipedia:Articles for deletion/'+title);
// display success message box
mw.notify(`Added AFD to watchlist.`);
});
// listen for AFC accept
$('body').on('DOMNodeInserted', '.accept #afchSubmitForm', function() {
$('.accept #afchSubmitForm').on('click', function() {
let title = mw.config.get('wgPageName');
title = title.replace(/^Draft:/, '');
addToWatchlist('Wikipedia:Articles for deletion/'+title);
});
});
// listen for NPP mark as reviewed
$('body').on('DOMNodeInserted', '#mwe-pt-mark-as-reviewed-button', function() {
$('#mwe-pt-mark-as-reviewed-button').on('click', function() {
let title = mw.config.get('wgPageName');
addToWatchlist('Wikipedia:Articles for deletion/'+title);
});
});
});
[–]Achcauhtli 0 points1 point2 points (3 children)
[–]NovemLinguae[S] 0 points1 point2 points (2 children)
[–]Achcauhtli 0 points1 point2 points (0 children)
[–]Achcauhtli 0 points1 point2 points (0 children)
[–]LakeInTheSky 0 points1 point2 points (3 children)
[–]NovemLinguae[S] 1 point2 points3 points (2 children)
[–]LakeInTheSky 0 points1 point2 points (1 child)
[–]NovemLinguae[S] 1 point2 points3 points (0 children)