-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make promise from web-socket API? #21
Comments
There is a non-standard API But you can just wrap it in a promise to turn it into a promise: const Market = require('some-api');
const promise = new Promise((resolve, reject) => {
const myMarket = new Market();
myMarket.websockets.trades(request[1], (trades, error) => {
if (error) {
reject(error);
return;
}
if (this.streamStatus === 1) {
resolve(JSON.stringify(['TR', this.getTimeString(), trades]));
} else {
reject(new Error('This promise wasnt handled'));
}
});
});
promise
.then((json) => { /* do stuff with json */ })
.catch((err) => { /* do stuff with err */ }) |
Thank you for your answer!
Yes, I also tried to write exactly this type of code in first place. And I check it now. It works very interesting - I get an expected string in the promise.then and nothing else, when this line:
myMarket.websockets.trades(request[1], (trades, error) => { ... } - it is websocket, I should get such lines every few seconds or milliseconds.
Do you have an idea how handle it?
Best, Alla
Web-site: http//allawitte.nl
…----- Reply to message -----
Subject: Re: [shama/letswritecode] How to make promise from web-socket API? (#21)
Date: 26 сентября 2018 г., 19:09:47
From: Kyle Robinson Young <[email protected]>
To: shama/letswritecode <[email protected]>
There is a non-standard API Promise.denodeify to help convert Node.js compatible callbacks to promises but that API signature in your example doesn't match the Node.js standard of error first in callbacks.
But you can just wrap it in a promise to turn it into a promise:
const Market = require('some-api');
const promise = new Promise((resolve, reject) => {
const myMarket = new Market();
myMarket.websockets.trades(request[1], (trades, error) => {
if (error) {
reject(error);
return;
}
if (this.streamStatus === 1) {
resolve(JSON.stringify(['TR', this.getTimeString(), trades]));
} else {
reject(new Error('This promise wasnt handled'));
}
});
});
promise
.then((json) => { /* do stuff with json */ })
.catch((err) => { /* do stuff with err */ })
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Ah ha, I see. If |
Thank you for your video!
I do application on nodejs what request some data on external server and relay them to some external client via TCP.
Do you have an idea how to make promise from the code like:
The text was updated successfully, but these errors were encountered: