What is the behavior of an observable within a forEach loop?What is the most efficient way to deep clone an object in JavaScript?How do I loop through or enumerate a JavaScript object?JavaScript closure inside loops – simple practical exampleWhat is the !! (not not) operator in JavaScript?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the difference between call and apply?What is JSONP, and why was it created?Loop through an array in JavaScriptWhat is TypeScript and why would I use it in place of JavaScript?What is the difference between Promises and Observables?

Fine-tuning parameters for existing methods

Story about two rival crews terraforming a planet

Should I cheat if the majority does it?

How should characters be punished for failing faction missions?

Magento 2: I am not aware about magneto optimization. Can you please share the steps for this?

"Best practices" for formulating MIPs

What is -(-2,3,4)?

Is my background sufficient to start Quantum Computing

When you're given a degree sequence, what is the method to draw a graph which has that degree sequence?

What is the difference between case and adpositions?

Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?

Should I hide my travel history to the UK when I apply for an Australian visa?

What could a Medieval society do with excess animal blood?

Why are symbols not written in words?

What is the point of using the kunai?

What does "another" mean in this case?

How is /a/ pronounced before n/m in French?

What instances can be solved today by modern solvers (pure LP)?

Turing Machines: What is the difference between recognizing, deciding, total, accepting, rejecting?

Will greasing clutch parts make it softer

Should I cross-validate metrics that were not optimised?

German idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)

What do you call the motor that fuels the movement of a robotic arm?

Go function to test whether a file exists



What is the behavior of an observable within a forEach loop?


What is the most efficient way to deep clone an object in JavaScript?How do I loop through or enumerate a JavaScript object?JavaScript closure inside loops – simple practical exampleWhat is the !! (not not) operator in JavaScript?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the difference between call and apply?What is JSONP, and why was it created?Loop through an array in JavaScriptWhat is TypeScript and why would I use it in place of JavaScript?What is the difference between Promises and Observables?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















When querying an observable within a forEach loop, does a new "instance" of the observable get created on every loop or does the existing observable get overwritten if the next loop query is requested before the previous observable returns a value?



For example, if I have messages to send to my users, I need to query for all their devices and send to all devices. An example of a topic in the topics array that is passed to createTopics(topics) would be:



Single Item in Topics Array




"submission":
"id": 52,
"artistID": 111,
"title": "Sego Sucks",
"Artist":
"name": "Sego"

,
"users": [

"id": 7,
"userUID": "ZvOBNBqxbgRYoibSYEwkL9YKtWG2"

]



My question is, when I run the forEach loop, I have an observable in findUserDevices that is iterated over several times really quickly, will my observable be "overwritten" by the next loop if it doesn't return a value before the next loop comes about? So far when I run this code, it executes as planned, but I am not sure if this is the best way to handle the observable as my data scales and there are more users per topic especially since pushToDevices() is an async function.



function createTopics(topics) 
topics.forEach((topic: any) =>
const message =
notification:
title: `New music from $topic.submission.Artist.name!`,
body: `Listen to "$topic.submission.title" now`



topic.users.forEach((user) =>
findUserDevices(user.userUID, message);
)
)


function findUserDevices(uid: string, message)
collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).subscribe((devices: any) =>
var userDeviceTokens: string[] = devices.map((device: any) => device.token);
if (userDeviceTokens.length != 0)
message['tokens'] = userDeviceTokens;
pushToDevices(message);

)


async function pushToDevices(message)
await admin.messaging().sendMulticast(message).then((response) =>
console.log('done!')
)



Thanks for any insight!










share|improve this question

















  • 2





    this will just fire off a bunch of async calls

    – Rafael
    Mar 25 at 17:59






  • 1





    "Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

    – SnorreDan
    Mar 25 at 18:26












  • Excellent thank you very much for the clarification both of you!

    – Jordan Lewallen
    Mar 25 at 18:40

















0















When querying an observable within a forEach loop, does a new "instance" of the observable get created on every loop or does the existing observable get overwritten if the next loop query is requested before the previous observable returns a value?



For example, if I have messages to send to my users, I need to query for all their devices and send to all devices. An example of a topic in the topics array that is passed to createTopics(topics) would be:



Single Item in Topics Array




"submission":
"id": 52,
"artistID": 111,
"title": "Sego Sucks",
"Artist":
"name": "Sego"

,
"users": [

"id": 7,
"userUID": "ZvOBNBqxbgRYoibSYEwkL9YKtWG2"

]



My question is, when I run the forEach loop, I have an observable in findUserDevices that is iterated over several times really quickly, will my observable be "overwritten" by the next loop if it doesn't return a value before the next loop comes about? So far when I run this code, it executes as planned, but I am not sure if this is the best way to handle the observable as my data scales and there are more users per topic especially since pushToDevices() is an async function.



function createTopics(topics) 
topics.forEach((topic: any) =>
const message =
notification:
title: `New music from $topic.submission.Artist.name!`,
body: `Listen to "$topic.submission.title" now`



topic.users.forEach((user) =>
findUserDevices(user.userUID, message);
)
)


function findUserDevices(uid: string, message)
collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).subscribe((devices: any) =>
var userDeviceTokens: string[] = devices.map((device: any) => device.token);
if (userDeviceTokens.length != 0)
message['tokens'] = userDeviceTokens;
pushToDevices(message);

)


async function pushToDevices(message)
await admin.messaging().sendMulticast(message).then((response) =>
console.log('done!')
)



Thanks for any insight!










share|improve this question

















  • 2





    this will just fire off a bunch of async calls

    – Rafael
    Mar 25 at 17:59






  • 1





    "Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

    – SnorreDan
    Mar 25 at 18:26












  • Excellent thank you very much for the clarification both of you!

    – Jordan Lewallen
    Mar 25 at 18:40













0












0








0








When querying an observable within a forEach loop, does a new "instance" of the observable get created on every loop or does the existing observable get overwritten if the next loop query is requested before the previous observable returns a value?



For example, if I have messages to send to my users, I need to query for all their devices and send to all devices. An example of a topic in the topics array that is passed to createTopics(topics) would be:



Single Item in Topics Array




"submission":
"id": 52,
"artistID": 111,
"title": "Sego Sucks",
"Artist":
"name": "Sego"

,
"users": [

"id": 7,
"userUID": "ZvOBNBqxbgRYoibSYEwkL9YKtWG2"

]



My question is, when I run the forEach loop, I have an observable in findUserDevices that is iterated over several times really quickly, will my observable be "overwritten" by the next loop if it doesn't return a value before the next loop comes about? So far when I run this code, it executes as planned, but I am not sure if this is the best way to handle the observable as my data scales and there are more users per topic especially since pushToDevices() is an async function.



function createTopics(topics) 
topics.forEach((topic: any) =>
const message =
notification:
title: `New music from $topic.submission.Artist.name!`,
body: `Listen to "$topic.submission.title" now`



topic.users.forEach((user) =>
findUserDevices(user.userUID, message);
)
)


function findUserDevices(uid: string, message)
collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).subscribe((devices: any) =>
var userDeviceTokens: string[] = devices.map((device: any) => device.token);
if (userDeviceTokens.length != 0)
message['tokens'] = userDeviceTokens;
pushToDevices(message);

)


async function pushToDevices(message)
await admin.messaging().sendMulticast(message).then((response) =>
console.log('done!')
)



Thanks for any insight!










share|improve this question














When querying an observable within a forEach loop, does a new "instance" of the observable get created on every loop or does the existing observable get overwritten if the next loop query is requested before the previous observable returns a value?



For example, if I have messages to send to my users, I need to query for all their devices and send to all devices. An example of a topic in the topics array that is passed to createTopics(topics) would be:



Single Item in Topics Array




"submission":
"id": 52,
"artistID": 111,
"title": "Sego Sucks",
"Artist":
"name": "Sego"

,
"users": [

"id": 7,
"userUID": "ZvOBNBqxbgRYoibSYEwkL9YKtWG2"

]



My question is, when I run the forEach loop, I have an observable in findUserDevices that is iterated over several times really quickly, will my observable be "overwritten" by the next loop if it doesn't return a value before the next loop comes about? So far when I run this code, it executes as planned, but I am not sure if this is the best way to handle the observable as my data scales and there are more users per topic especially since pushToDevices() is an async function.



function createTopics(topics) 
topics.forEach((topic: any) =>
const message =
notification:
title: `New music from $topic.submission.Artist.name!`,
body: `Listen to "$topic.submission.title" now`



topic.users.forEach((user) =>
findUserDevices(user.userUID, message);
)
)


function findUserDevices(uid: string, message)
collectionData(fb.firestore().collection('devices').where('userId', '==', uid)).subscribe((devices: any) =>
var userDeviceTokens: string[] = devices.map((device: any) => device.token);
if (userDeviceTokens.length != 0)
message['tokens'] = userDeviceTokens;
pushToDevices(message);

)


async function pushToDevices(message)
await admin.messaging().sendMulticast(message).then((response) =>
console.log('done!')
)



Thanks for any insight!







javascript typescript rxjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 17:56









Jordan LewallenJordan Lewallen

5424 silver badges12 bronze badges




5424 silver badges12 bronze badges







  • 2





    this will just fire off a bunch of async calls

    – Rafael
    Mar 25 at 17:59






  • 1





    "Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

    – SnorreDan
    Mar 25 at 18:26












  • Excellent thank you very much for the clarification both of you!

    – Jordan Lewallen
    Mar 25 at 18:40












  • 2





    this will just fire off a bunch of async calls

    – Rafael
    Mar 25 at 17:59






  • 1





    "Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

    – SnorreDan
    Mar 25 at 18:26












  • Excellent thank you very much for the clarification both of you!

    – Jordan Lewallen
    Mar 25 at 18:40







2




2





this will just fire off a bunch of async calls

– Rafael
Mar 25 at 17:59





this will just fire off a bunch of async calls

– Rafael
Mar 25 at 17:59




1




1





"Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

– SnorreDan
Mar 25 at 18:26






"Does a new instance of the observable get created on every loop?", the answer to that question is yes. This means that you can safely instantiate a new Observable for each iteration of your loop like you are doing here, there is nothing wrong with doing that. It will not overwrite the observable in the previous loop.

– SnorreDan
Mar 25 at 18:26














Excellent thank you very much for the clarification both of you!

– Jordan Lewallen
Mar 25 at 18:40





Excellent thank you very much for the clarification both of you!

– Jordan Lewallen
Mar 25 at 18:40












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343877%2fwhat-is-the-behavior-of-an-observable-within-a-foreach-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55343877%2fwhat-is-the-behavior-of-an-observable-within-a-foreach-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh