I'm writing a Twitter™ bot for the Retrocomputing Stack Exchange site using JavaScript. To ensure that it doesn't tweet about the same question twice in a row, I request the account's timeline. My code should work, but I have a feeling that there's something "wrong" with the way I've written it.

The get_request function is a wrapper for XMLHttpRequest that returns a Promise object. It takes one url parameter, and an optional responsetype parameter that is currently unused by get_twitter_timeline_fragments. The get_twitter_timeline_fragments function is initially called with only the userid and count parameters, as maxid and fragments are used internally. The function doesn't call itself recursively because of the behaviour of Promise.

Here is the get_twitter_timeline_fragments function:

function get_twitter_timeline_fragments(userid, count, maxid, fragments) {
    if (fragments === undefined) {
        fragments = [];
    return get_request(
        "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=" + userid +
        "&count=" + ((count > 200) ? 200 : count) +
        ((maxid === undefined) ? "" : ("&max_id=" + maxid))
    ).then(function(response) {
        qobj = JSON.parse(response.responseText);
        if (qobj.length >= count) {
            return fragments;
        } else {
            return get_twitter_timeline_fragments(userid, count - qobj.length, qobj[qobj.length - 1].id_str, fragments);

To use the result of the function would require get_twitter_timeline_fragments(userid, count).then(f) where f=x=>isPromise(x)?x.then(f):x; that doesn't seem like a big problem because I'm going to wrap this function in one that processes the data.

The full code is located here. To run the code on the test page you must "Allow All" on your ad-blocker, use a "Same Origin" spoofer (e.g. CORS everywhere) and pre-authenticate to the Twitter API with an API key (so the Twitter side works).



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.