'Parsing JSON from Wikipedia API throws an error, but only a quarter of the time

I'm trying to get the summary of a random article using the Wikipedia API, but for some reason when parsing the JSON, I run into this error about a quarter of the time: Unexpected token < in JSON at position 0. Why is this happening? I've pasted the link into my browser manually multiple times and haven't seen anything wrong with the JSON string.

const articleCount = 10;

const fetch = require('sync-fetch');
function article()
{
    return fetch('https://en.wikipedia.org/api/rest_v1/page/random/summary').json().extract;
}

var source = '';
for(let i = 0; i < articleCount; i++)
{
    source += article() + ' ';
    console.log(parseInt(i/articleCount * 100) + '%');
}

console.log(source);

I've switched out the .json() method with JSON.parse(string), but it has the same problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source