Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Feb 01 00:27
    @bjorno43 banned @Ndoua
  • Jan 07 03:10
    @bjorno43 banned @doctor-sam
  • Aug 17 2019 09:32
    User @bjorno43 unbanned @linkin-park
  • Jun 20 2019 21:19
    @bjorno43 banned @shenerd140
  • Apr 25 2019 17:13
    @mstellaluna banned @cmal
  • Jan 08 2019 22:05
    @mstellaluna banned @dertiuss323
  • Nov 07 2018 04:42
    User @texas2010 unbanned @ows-ali
  • Nov 02 2018 16:25
    @texas2010 banned @ows-ali
  • Oct 12 2018 05:50
    @bjorno43 banned @NACH74
  • Oct 05 2018 23:02
    @mstellaluna banned @JomoPipi
  • Sep 16 2018 12:21
    @bjorno43 banned @yash-kedia
  • Sep 16 2018 12:16
    @bjorno43 banned @vnikifirov
  • Sep 05 2018 08:12
    User @bjorno43 unbanned @androuino
  • Sep 05 2018 07:38
    @bjorno43 banned @androuino
  • Aug 23 2018 16:57
    User @bjorno43 unbanned @rahuldkjain
  • Aug 23 2018 16:23
    @bjorno43 banned @rahuldkjain
  • Jul 29 2018 14:13
    User @bjorno43 unbanned @jkyereh
  • Jul 29 2018 01:00
    @bjorno43 banned @jkyereh
  • Jul 10 2018 22:09
    @bjorno43 banned @manafn
  • Jul 06 2018 15:20
    @texas2010 banned @imlegend19
siliconchild
@siliconchild
can someone help me with an async function that has a loop and filter
Kaz Baig
@kbaig
sup
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const productsList = [];
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
    categoriesIdArray.forEach(async categoryID => {
        const response = await getSearchResults(" ", categoryID);
        productsList.push(...response);
    });
    return productsList;
 }

const hotDeals = async () => {
    const products = await fetchAllProducts();
    return products.forEach(product => product.MRP/product.SellingPrice > .8)
}
Brad
@bradtaniguchi
what behavior do you seek: make the calls and don't wait for anything, or wait for all calls and do something, or make each call waiting for the previous
Looks like you want the 2nd one, where you make all the calls and wait for all of them to be done. Id's use Promise.all
and the .map, without async/await
return Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
@siliconchild
siliconchild
@siliconchild
@bradtaniguchi thank a lot, let me try that
Brad
@bradtaniguchi
Just a word of warning, I don't advise this approach for large lists. Not because the code is wrong, its that if your on the client-side making X number of calls things can slow dramatically
siliconchild
@siliconchild
@bradtaniguchi i understand that, i am thinking to run this a cloud function on netlify that i can call
i dont have access to the server api
to create new apis there
Brad
@bradtaniguchi
Ah, if your calling an API it isn't as bad, but still non-optimal if the list is large. If the api your interfacing with is designed for it tho, then it shouldn't matter too much
siliconchild
@siliconchild
@bradtaniguchi yes this is hacky solution, since there is no api to fetch deals, i am writing this intermediary api to create one. is there a better solution if i can't create an api in the original server
@bradtaniguchi is the issue with this, creating too many reads of the database?
Brad
@bradtaniguchi
Well for each request you make you go through the "entire flow" of a request in the api, so the webserver is called, logic is checked, potentially multiple database calls are called (for security+your data) and thats for each call
siliconchild
@siliconchild
@bradtaniguchi so this would bog down the server considerably?
the list has currently less than 1000 items
Brad
@bradtaniguchi
Whoa, it could get up to 1k calls? :O
Its impact on the server depends on how its implemented
if its a nodejs server, and lets say there is 0 security, your still looking at 1k individual database calls, and 1k router hits all at the same time. Heck that could bring up DDOS protection with some web servers (idk if that applies here tho)
siliconchild
@siliconchild
@bradtaniguchi i don't much about server side, but when this logic is written on the server side, woudn't the all the data needs to read to filter and create this api?
so that would create a similar load on the database right?
Brad
@bradtaniguchi
Not sure what you mean by "woudn't the all the data needs to read to filter and create this api?"
Kaz Baig
@kbaig
What brad said, you're going to be waiting for each call to end before moving on to the next one in the array unless you use Promise.all
siliconchild
@siliconchild
so promise.all runs in parallel ?
Brad
@bradtaniguchi
yes
Kaz Baig
@kbaig
yep
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
    return Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
 }
is this enough, this is still returning a promise
Brad
@bradtaniguchi
Lets say you had access to the DB yourself, you'd make 1 call/request to it with a query, and get 1 response back with all your data. Its a lot less processing to handle the network transfers at each level
Your in an async function, it always is a promise
siliconchild
@siliconchild
ok so i just need to write another async function to await fetchAllProducts?
Brad
@bradtaniguchi
what? I thought thats what you already had?
const hotDeals = async () => {
    const products = await fetchAllProducts();
    return products.forEach(product => product.MRP/product.SellingPrice > .8)
}
siliconchild
@siliconchild
yes i have that
Brad
@bradtaniguchi
yea that stays the same
siliconchild
@siliconchild
ok, what you said about the database makes sense
thank you
Brad
@bradtaniguchi
np, there are a few ways you can cut down on calls that lean on the fact your netlify api can "cache" previous values, but this increases complexity and creates a layer of caching that needs to be cleared
siliconchild
@siliconchild
so i can cache the products list for an hour until i update it again
that is a good idea
Brad
@bradtaniguchi
netlify will automatically "clears the cache" if the server turns off aswell so there is that
siliconchild
@siliconchild
btw the server admin said, to implement this feature it is too much work for him and he would need to rewrite a lot of code, i don't know if he is bluffing or if it actually takes a lot of work to create a new api like this, could you tell me your opinion
Kaz Baig
@kbaig
We don't know what goes into building on api on your platform tho
Brad
@bradtaniguchi
Couldn't be that bad honestly, even if we cranked up the complexity to 100, being able to do an "OR" request essentially it seems kinda important
unless of course the API is just optimized for the individual api calls
siliconchild
@siliconchild
const fetchAllProducts = async () => {
    const categories = await GetActiveCategories();
    const categoriesIdArray = categories.map(category => category.CategoryId);
    return Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
 }

const hotDeals = async () => {
    const products = await fetchAllProducts();
    console.log('PRODUCT DATA', products)
    return products.forEach(product => product.MRP/product.SellingPrice > .8)
}

const displayHotDeals = async () => {
    const data = await hotDeals();
    console.log('DATA',data)
}

displayHotDeals();
the filter is returning undefined
the first console.log gives out the array