0

I'm fairly new to JS, this being my first project in it (using sqlite and the twitter api). I've been stuck for a while now on this issue. I can get await and async to work fine in other test programs but when it comes to this one the await within the following function isn't waiting for getmin() to conclude before moving on, which is causing issues in my for loop.

In the same vein like I said this is my first js project, it's highly likely to be wildly unoptimised. If something stands out as inefficient to you let me know and I'll know for future what to do/avoid.

async function following(err, data, response) {
if (err) {
    console.log(err)
} else {
    for (let i = 0; i < data.users.length; i++){
        let user = data.users[i]
        userinfo.name = user.name
        userinfo.screen_name = user.screen_name
        userinfo.user_ID = user.id
        maxparams.user_id = user.id
        new_min_params.user_id = user.id
        console.log(new_min_params)
        x = await getmin()
        console.log(x)
        // T.get('statuses/user_timeline', maxparams, getmax)
        // T.get('statuses/user_timeline', tweetparams, gettweets)
        //databasedata = [data.users.screen_name, minTweetID, maxTweetID]
    }
    // console.log(response_dictionary)
    // console.log("")
    cursorscore = 0
    }

function getmin() {
    let sql = `SELECT MaxTweetID FROM following WHERE Handle = ?`;
    db.get(sql, [userinfo.screen_name], (err, row) => {
    if (err) {
        return console.error(err.message);
    } else {
        return (row ? updatemin(userinfo.screen_name) : T.get('statuses/user_timeline', new_min_params, get_tenth_tweet))
}});

}

  • Your `getmin` function needs to return a promise. Although hidden by the inconsistent indentation, the `return` statements inside the `get` callback do not return from `getmin`. – Bergi Jul 04 '20 at 18:30
  • Are values returned in this way not automatically converted into resolved promise form? eg. if I had 2 functions, the first which awaited the second and once it had finished waiting printed x, the result of the returned function. The second function waited 2 seconds and then returned a number, say 5. Does the code awaiting the resolve not see this as resolve(5)? – Conor Fleury Jul 05 '20 at 01:40
  • No, callbacks (like the one in `getmin`) do not automatically get converted into promise form. – Bergi Jul 05 '20 at 17:44

0 Answers0