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;
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
add a comment |
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
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
add a comment |
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
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
javascript typescript rxjs
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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