Created
June 10, 2018 21:08
-
-
Save max8hine/829097d73d7c0318e62fd930546a4574 to your computer and use it in GitHub Desktop.
Pausing Async fetching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Method 1 | |
planList() | |
.then((planRes) => { | |
return planRes.data.plan | |
// locals.addOnUrlList = planRes.data.plan.map(obj => _.pick(obj, 'add_ons').add_ons.href) | |
// done() | |
}) | |
.then((planListRes) => { | |
locals.planList = planListRes | |
const promises = planListRes.map(item => planAddonList(item.add_ons.href.split('/v2/')[1])) | |
return Promise.all(promises) | |
}) | |
.then((addOnListRes) => { | |
addOnListRes.forEach((v) => { | |
locals.addOnList.push(v.data) | |
}) | |
done() | |
}) | |
.catch((planErr) => { | |
done(errorMsg(planErr)) | |
}) | |
// Method 2 | |
function (done) { | |
locals.addOnList = [] | |
async function fetchAddons() { | |
// list of promise | |
const addOnListPromise = locals.planList.map(async (item) => { | |
try { | |
return await planAddonList(item.add_ons.href.split('/v2/')[1]) | |
} catch (error) { | |
done(errorMsg(error)) | |
return | |
} | |
}) | |
try { | |
const fullResAddOnList = await Promise.all(addOnListPromise) | |
fullResAddOnList.forEach(v => { | |
locals.addOnList.push(v.data) | |
}) | |
} catch (error) { | |
done(errorMsg(error)) | |
} | |
done() | |
} | |
fetchAddons() | |
}, | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment