mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-12-12 00:31:11 -05:00
30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
|
const api = require('./api');
|
||
|
|
||
|
module.exports = {};
|
||
|
|
||
|
module.exports.requestSessionWithRetry = (count, resolve, reject) => {
|
||
|
console.log('starting refreshSessionWithRetry');
|
||
|
api({
|
||
|
host: '',
|
||
|
uri: '/session/'
|
||
|
}, (err, body, response) => {
|
||
|
if (err || (response && response.statusCode === 404)) {
|
||
|
console.log('refreshSessionWithRetry: resolving with session err');
|
||
|
return reject(err);
|
||
|
}
|
||
|
if (typeof body === 'undefined' || !body.user) {
|
||
|
// Retry after 500ms, 1.5s, 3.5s, 7.5s and then stop.
|
||
|
if (count > 4) {
|
||
|
console.log('refreshSessionWithRetry: too many tries, resolving');
|
||
|
return resolve(body);
|
||
|
}
|
||
|
console.log(`with count ${count}, waiting ${(250 * Math.pow(2, count))} ms`);
|
||
|
return setTimeout(module.exports.requestSessionWithRetry.bind(null, count + 1, resolve, reject),
|
||
|
250 * Math.pow(2, count));
|
||
|
}
|
||
|
console.log('refreshSessionWithRetry: session found! resolving with body:');
|
||
|
// console.log(body);
|
||
|
return resolve(body);
|
||
|
});
|
||
|
};
|