From 0626e6d426696b263006acc8d6f6bb0e166cccd6 Mon Sep 17 00:00:00 2001 From: fenwick67 Date: Wed, 31 Jul 2019 15:21:31 -0400 Subject: [PATCH] fetch user and feed in parallel when I can --- lib/convertv2.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/convertv2.js b/lib/convertv2.js index 4b6c0dc..0e6235a 100644 --- a/lib/convertv2.js +++ b/lib/convertv2.js @@ -5,6 +5,7 @@ var timeAgo = require('timeago.js'); var request = require('request-promise-native') // get JSON for an AP URL +// TODO make it reject on HTTP 4xx or 5xx async function apGet(url) { return request.get( { uri:url, @@ -17,8 +18,6 @@ async function apGet(url) { }) } -// accumulate a stream of XML into a html file - module.exports = async function (opts) { var opts = opts; @@ -30,16 +29,26 @@ module.exports = async function (opts) { throw new Error('need user URL'); } - var user = await apGet(userUrl); + var user, feed; - if (userUrl && !feedUrl) { - isIndex = true; - var outbox = await apGet(user.outbox); - feedUrl = outbox.first; + // get user and feed in parallel if I have both URLs + if (userUrl && feedUrl){ + [user, feed] = await Promise.all([ apGet(userUrl), apGet(feedUrl) ]); + }else{ + // get user, then outbox, then feed + + user = await apGet(userUrl); + + if (userUrl && !feedUrl) { + isIndex = true; + var outbox = await apGet(user.outbox); + feedUrl = outbox.first; + } + + feed = await apGet(feedUrl); } - var feed = await apGet(feedUrl); var templateData = { opts: opts,// from the request