I have some really heavy promise based code, that I'm looking to break up, and move over to async/await.
I'm running into an issue however - what's a good design pattern for the following problem. Essentially I have something that is reliant on the previous step of the 'promise'.
How can I pass the value down to the block below? Or just init it to something so I can keep it around?
Thanks, Ollie
let sess;
// if has experts
if (!_.isEmpty(data.expertIds)) {
// create expert campaign sessions
const sessions = await util.createSessions(data.expertIds, newCampaignId);
Promise.all(sessions)
.then((s) => {
// if sessions created, set this var to true!
sess = true;
debug('Sessions created');
})
.catch((error) => { throw error; });
}
debug(sess) // undefined
debug(await sess) // undefined
...lots and losts more code...
let newCampaignId;
// save the new campaign as a draft
newCampaign
.save()
.then((campaign) => {
newCampaignId = campaign._id;
debug('New campaign saved', campaign);
})
.catch((error) => { throw error; });
debug(newCampaignId) // undefined
debug(await newCampaignId) // undefined
s = await Promise.all(sessions)