what is the best method to multi request with callback or promise on node.js
I have a situation. I use Node.js to connect to a special hardware. let assume that I have two functions to access the hardware.
hardware.send('command');
hardware.on('responce', callback);
At first, I made a class to interface this to the application layer like this (I write simplified code over here for better understanding)
class AccessHardware {
constructor() {
}
updateData(callback) {
hardware.on('responce', callback);
hardware.send('command');
}
}
Now, the problem is that if there are multiple requests from the application layer to this access layer, they should not send multiple 'command' to the hardware. Instead, they should send one command and all of those callbacks can be served once the hardware answer the command.
So I update the code something like this:
class AccessHardware {
constructor() {
this.callbackList = ;
hardware.on('responce', (value) => {
while (this.callbackList.length > 0) {
this.callbackList.pop()(value);
}
});
}
updateData(callback) {
if (this.callbackList.length == 0) {
hardware.send('command');
}
this.callbackList.push(callback);
}
}
Of course, I prefer to use promise to handle the situation. so what is your suggestion to write this code with promise?
Next question, is this approach to make a 'list of callbacks' good?
node.js list callback promise
add a comment |
I have a situation. I use Node.js to connect to a special hardware. let assume that I have two functions to access the hardware.
hardware.send('command');
hardware.on('responce', callback);
At first, I made a class to interface this to the application layer like this (I write simplified code over here for better understanding)
class AccessHardware {
constructor() {
}
updateData(callback) {
hardware.on('responce', callback);
hardware.send('command');
}
}
Now, the problem is that if there are multiple requests from the application layer to this access layer, they should not send multiple 'command' to the hardware. Instead, they should send one command and all of those callbacks can be served once the hardware answer the command.
So I update the code something like this:
class AccessHardware {
constructor() {
this.callbackList = ;
hardware.on('responce', (value) => {
while (this.callbackList.length > 0) {
this.callbackList.pop()(value);
}
});
}
updateData(callback) {
if (this.callbackList.length == 0) {
hardware.send('command');
}
this.callbackList.push(callback);
}
}
Of course, I prefer to use promise to handle the situation. so what is your suggestion to write this code with promise?
Next question, is this approach to make a 'list of callbacks' good?
node.js list callback promise
So allupdateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?
– Bergi
Nov 24 '18 at 11:59
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57
add a comment |
I have a situation. I use Node.js to connect to a special hardware. let assume that I have two functions to access the hardware.
hardware.send('command');
hardware.on('responce', callback);
At first, I made a class to interface this to the application layer like this (I write simplified code over here for better understanding)
class AccessHardware {
constructor() {
}
updateData(callback) {
hardware.on('responce', callback);
hardware.send('command');
}
}
Now, the problem is that if there are multiple requests from the application layer to this access layer, they should not send multiple 'command' to the hardware. Instead, they should send one command and all of those callbacks can be served once the hardware answer the command.
So I update the code something like this:
class AccessHardware {
constructor() {
this.callbackList = ;
hardware.on('responce', (value) => {
while (this.callbackList.length > 0) {
this.callbackList.pop()(value);
}
});
}
updateData(callback) {
if (this.callbackList.length == 0) {
hardware.send('command');
}
this.callbackList.push(callback);
}
}
Of course, I prefer to use promise to handle the situation. so what is your suggestion to write this code with promise?
Next question, is this approach to make a 'list of callbacks' good?
node.js list callback promise
I have a situation. I use Node.js to connect to a special hardware. let assume that I have two functions to access the hardware.
hardware.send('command');
hardware.on('responce', callback);
At first, I made a class to interface this to the application layer like this (I write simplified code over here for better understanding)
class AccessHardware {
constructor() {
}
updateData(callback) {
hardware.on('responce', callback);
hardware.send('command');
}
}
Now, the problem is that if there are multiple requests from the application layer to this access layer, they should not send multiple 'command' to the hardware. Instead, they should send one command and all of those callbacks can be served once the hardware answer the command.
So I update the code something like this:
class AccessHardware {
constructor() {
this.callbackList = ;
hardware.on('responce', (value) => {
while (this.callbackList.length > 0) {
this.callbackList.pop()(value);
}
});
}
updateData(callback) {
if (this.callbackList.length == 0) {
hardware.send('command');
}
this.callbackList.push(callback);
}
}
Of course, I prefer to use promise to handle the situation. so what is your suggestion to write this code with promise?
Next question, is this approach to make a 'list of callbacks' good?
node.js list callback promise
node.js list callback promise
asked Nov 24 '18 at 11:39
Behnam GhiaseddinBehnam Ghiaseddin
6316
6316
So allupdateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?
– Bergi
Nov 24 '18 at 11:59
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57
add a comment |
So allupdateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?
– Bergi
Nov 24 '18 at 11:59
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57
So all
updateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?– Bergi
Nov 24 '18 at 11:59
So all
updateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?– Bergi
Nov 24 '18 at 11:59
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57
add a comment |
1 Answer
1
active
oldest
votes
I prefer to use promise to handle the situation. So what is your suggestion to write this code with promise?
You'd store a promise in your instance that will be shared between all method callers that want to share the same result:
class AccessHardware {
constructor(hardware) {
this.hardware = hardware;
this.responsePromise = null;
}
updateData() {
if (!this.responsePromise) {
this.responsePromise = new Promise(resolve => {
this.hardware.on('responce', resolve);
this.hardware.send('command');
});
this.responsePromise.finally(() => {
this.responsePromise = null; // clear cache as soon as command is done
});
}
return this.responsePromise;
}
}
Btw, if hardware
is a global variable, there's no reason to use a class
here.
Is the current solution to make a 'list of callbacks' good?
Yes, that's fine as well for a non-promise approach.
add a comment |
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%2f53457732%2fwhat-is-the-best-method-to-multi-request-with-callback-or-promise-on-node-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I prefer to use promise to handle the situation. So what is your suggestion to write this code with promise?
You'd store a promise in your instance that will be shared between all method callers that want to share the same result:
class AccessHardware {
constructor(hardware) {
this.hardware = hardware;
this.responsePromise = null;
}
updateData() {
if (!this.responsePromise) {
this.responsePromise = new Promise(resolve => {
this.hardware.on('responce', resolve);
this.hardware.send('command');
});
this.responsePromise.finally(() => {
this.responsePromise = null; // clear cache as soon as command is done
});
}
return this.responsePromise;
}
}
Btw, if hardware
is a global variable, there's no reason to use a class
here.
Is the current solution to make a 'list of callbacks' good?
Yes, that's fine as well for a non-promise approach.
add a comment |
I prefer to use promise to handle the situation. So what is your suggestion to write this code with promise?
You'd store a promise in your instance that will be shared between all method callers that want to share the same result:
class AccessHardware {
constructor(hardware) {
this.hardware = hardware;
this.responsePromise = null;
}
updateData() {
if (!this.responsePromise) {
this.responsePromise = new Promise(resolve => {
this.hardware.on('responce', resolve);
this.hardware.send('command');
});
this.responsePromise.finally(() => {
this.responsePromise = null; // clear cache as soon as command is done
});
}
return this.responsePromise;
}
}
Btw, if hardware
is a global variable, there's no reason to use a class
here.
Is the current solution to make a 'list of callbacks' good?
Yes, that's fine as well for a non-promise approach.
add a comment |
I prefer to use promise to handle the situation. So what is your suggestion to write this code with promise?
You'd store a promise in your instance that will be shared between all method callers that want to share the same result:
class AccessHardware {
constructor(hardware) {
this.hardware = hardware;
this.responsePromise = null;
}
updateData() {
if (!this.responsePromise) {
this.responsePromise = new Promise(resolve => {
this.hardware.on('responce', resolve);
this.hardware.send('command');
});
this.responsePromise.finally(() => {
this.responsePromise = null; // clear cache as soon as command is done
});
}
return this.responsePromise;
}
}
Btw, if hardware
is a global variable, there's no reason to use a class
here.
Is the current solution to make a 'list of callbacks' good?
Yes, that's fine as well for a non-promise approach.
I prefer to use promise to handle the situation. So what is your suggestion to write this code with promise?
You'd store a promise in your instance that will be shared between all method callers that want to share the same result:
class AccessHardware {
constructor(hardware) {
this.hardware = hardware;
this.responsePromise = null;
}
updateData() {
if (!this.responsePromise) {
this.responsePromise = new Promise(resolve => {
this.hardware.on('responce', resolve);
this.hardware.send('command');
});
this.responsePromise.finally(() => {
this.responsePromise = null; // clear cache as soon as command is done
});
}
return this.responsePromise;
}
}
Btw, if hardware
is a global variable, there's no reason to use a class
here.
Is the current solution to make a 'list of callbacks' good?
Yes, that's fine as well for a non-promise approach.
answered Nov 24 '18 at 15:04
BergiBergi
373k59562893
373k59562893
add a comment |
add a comment |
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%2f53457732%2fwhat-is-the-best-method-to-multi-request-with-callback-or-promise-on-node-js%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
So all
updateData()
calls send the same command, and they shouldn't send a command while one still is in flight? So that you get the maximum rate of commands being sent, with results being arbitrarily being shared amongst consecutive calls? Do you expect the results to change? If yes, sharing doesn't sound like a good idea, if no, why do you want to send multiple commands at all instead of sharing the single result?– Bergi
Nov 24 '18 at 11:59
@Bergi well, application layer has multiple web-based clients, so it's possible that they all ask the same command. After the result becomes ready (it takes about 15 seconds) application should send the result to all clients. that is the reason all commands are same but there are multiple requests
– Behnam Ghiaseddin
Nov 24 '18 at 13:45
Yes, but if another clients asks the same command after 30s, would it really need to run the command again? Couldn't it just return the already available result that got sent to the other clients before?
– Bergi
Nov 24 '18 at 14:29
@Bergi no, it should always get a new query because the state of the hardware may get changed during this time.
– Behnam Ghiaseddin
Nov 24 '18 at 14:53
OK, it's just that when the state of the hardware changes after 10s (while the first command is still running) a client that asks at 15s still gets the old result. This seemed somewhat arbitrary to me, I'd rather recommend a generic debounce function for that which clears its cache after a fixed time.
– Bergi
Nov 24 '18 at 14:57