Convert a stream to PromiseFile stream functionsCaching/memoizing promise resultsMongoose promise & error...

Find x angle in triangle

How to prevent users from executing commands through browser URL

How do I say "Brexit" in Latin?

Every character has a name - does this lead to too many named characters?

Can you combine War Caster, whip, and Warlock Features to EB enemies with reach?

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

Calculate Contact age in a Drupal view

What is the wife of a henpecked husband called?

Is there any differences between "Gucken" and "Schauen"?

Slow moving projectiles from a hand-held weapon - how do they reach the target?

Why is working on the same position for more than 15 years not a red flag?

Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?

Why Normality assumption in linear regression

Guns against regular people

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

How do you funnel food off a cutting board?

What was the earliest start time of a Catholic mass before 1957?

We are very unlucky in my court

Using only 1s, make 29 with the minimum number of digits

Checking for the existence of multiple directories

What is the most triangles you can make from a capital "H" and 3 straight lines?

What's a good word to describe a public place that looks like it wouldn't be rough?

Would these multi-classing house rules cause unintended problems?

Dilemma of explaining to interviewer that he is the reason for declining second interview



Convert a stream to Promise


File stream functionsCaching/memoizing promise resultsMongoose promise & error handlingExiting a Promise earlyFile existence promiseNode.js stream of objects from MongoDBMemory streambuf and streamNode.js Sequelize PromiseCode refactor with promiseStream wrapper in node.ts













0












$begingroup$


I need to collect all the data from the stream and resolve it as promise.



I created this stream:



class CollectToPromise extends Writable {
constructor(options) {
super();
this._result = ""

this.result = new Promise((resolve, reject) => {
this.on('finish', resolve):w
this.on('error', reject)
}).then(() => this._result)

}

_write(chunk, encoding, callback) {
this._result += chunk
callback()
}

_writev(chunks, callback) {
chunks.forEach(c => {
this._result += chunk
})
callback()
}

asPromise() {
return this.result
}
}


and I use it like this:



var r = fs.createReadStream('secrets');
const c = new CollectToPromise()
const encrypt = crypto.createCipher(algorithm, password);
r.pipe(encrypt).pipe(c).asPromise().then((res) => console.log(res)


My questions are:




  1. What do you think about the solution, including the naming?

  2. How can I avoid the warning: (node:32386) Warning: Use Cipheriv for counter mode of aes-256-ctr?










share|improve this question











$endgroup$




bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • $begingroup$
    asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:23












  • $begingroup$
    >then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:43






  • 1




    $begingroup$
    Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:50












  • $begingroup$
    Ok, now I see your point, thx
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:53












  • $begingroup$
    Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
    $endgroup$
    – guest271314
    Jan 29 at 8:41


















0












$begingroup$


I need to collect all the data from the stream and resolve it as promise.



I created this stream:



class CollectToPromise extends Writable {
constructor(options) {
super();
this._result = ""

this.result = new Promise((resolve, reject) => {
this.on('finish', resolve):w
this.on('error', reject)
}).then(() => this._result)

}

_write(chunk, encoding, callback) {
this._result += chunk
callback()
}

_writev(chunks, callback) {
chunks.forEach(c => {
this._result += chunk
})
callback()
}

asPromise() {
return this.result
}
}


and I use it like this:



var r = fs.createReadStream('secrets');
const c = new CollectToPromise()
const encrypt = crypto.createCipher(algorithm, password);
r.pipe(encrypt).pipe(c).asPromise().then((res) => console.log(res)


My questions are:




  1. What do you think about the solution, including the naming?

  2. How can I avoid the warning: (node:32386) Warning: Use Cipheriv for counter mode of aes-256-ctr?










share|improve this question











$endgroup$




bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • $begingroup$
    asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:23












  • $begingroup$
    >then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:43






  • 1




    $begingroup$
    Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:50












  • $begingroup$
    Ok, now I see your point, thx
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:53












  • $begingroup$
    Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
    $endgroup$
    – guest271314
    Jan 29 at 8:41
















0












0








0


1



$begingroup$


I need to collect all the data from the stream and resolve it as promise.



I created this stream:



class CollectToPromise extends Writable {
constructor(options) {
super();
this._result = ""

this.result = new Promise((resolve, reject) => {
this.on('finish', resolve):w
this.on('error', reject)
}).then(() => this._result)

}

_write(chunk, encoding, callback) {
this._result += chunk
callback()
}

_writev(chunks, callback) {
chunks.forEach(c => {
this._result += chunk
})
callback()
}

asPromise() {
return this.result
}
}


and I use it like this:



var r = fs.createReadStream('secrets');
const c = new CollectToPromise()
const encrypt = crypto.createCipher(algorithm, password);
r.pipe(encrypt).pipe(c).asPromise().then((res) => console.log(res)


My questions are:




  1. What do you think about the solution, including the naming?

  2. How can I avoid the warning: (node:32386) Warning: Use Cipheriv for counter mode of aes-256-ctr?










share|improve this question











$endgroup$




I need to collect all the data from the stream and resolve it as promise.



I created this stream:



class CollectToPromise extends Writable {
constructor(options) {
super();
this._result = ""

this.result = new Promise((resolve, reject) => {
this.on('finish', resolve):w
this.on('error', reject)
}).then(() => this._result)

}

_write(chunk, encoding, callback) {
this._result += chunk
callback()
}

_writev(chunks, callback) {
chunks.forEach(c => {
this._result += chunk
})
callback()
}

asPromise() {
return this.result
}
}


and I use it like this:



var r = fs.createReadStream('secrets');
const c = new CollectToPromise()
const encrypt = crypto.createCipher(algorithm, password);
r.pipe(encrypt).pipe(c).asPromise().then((res) => console.log(res)


My questions are:




  1. What do you think about the solution, including the naming?

  2. How can I avoid the warning: (node:32386) Warning: Use Cipheriv for counter mode of aes-256-ctr?







javascript node.js stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 23 '18 at 0:43









Jamal

30.3k11119227




30.3k11119227










asked Dec 6 '18 at 11:15









kharandziukkharandziuk

4371316




4371316





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • $begingroup$
    asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:23












  • $begingroup$
    >then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:43






  • 1




    $begingroup$
    Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:50












  • $begingroup$
    Ok, now I see your point, thx
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:53












  • $begingroup$
    Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
    $endgroup$
    – guest271314
    Jan 29 at 8:41




















  • $begingroup$
    asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:23












  • $begingroup$
    >then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:43






  • 1




    $begingroup$
    Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
    $endgroup$
    – t.niese
    Dec 6 '18 at 12:50












  • $begingroup$
    Ok, now I see your point, thx
    $endgroup$
    – kharandziuk
    Dec 6 '18 at 12:53












  • $begingroup$
    Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
    $endgroup$
    – guest271314
    Jan 29 at 8:41


















$begingroup$
asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
$endgroup$
– t.niese
Dec 6 '18 at 12:23






$begingroup$
asPromise can be problematic depending on how you implement it. If you create the promise before asPromise is called, and if you reject that Promise due to an error occuring in th stream, then you can get a unhandled promise rejection warning if your code does not use .asPromise at all, and that waring will result in a real error in future that will exit the application.
$endgroup$
– t.niese
Dec 6 '18 at 12:23














$begingroup$
>then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
$endgroup$
– kharandziuk
Dec 6 '18 at 12:43




$begingroup$
>then you can get a unhandled promise rejection warning if your code does not use it's probably a downside of using any promise
$endgroup$
– kharandziuk
Dec 6 '18 at 12:43




1




1




$begingroup$
Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
$endgroup$
– t.niese
Dec 6 '18 at 12:50






$begingroup$
Tha naming asPromise implies that this Promise is created on demand when asPromise is called. And not that it already exist before that call. So writing only r.pipe(encrypt).pipe(c); should at least not result in a unhandled promise rejection warning. Just because it would break the expectation.
$endgroup$
– t.niese
Dec 6 '18 at 12:50














$begingroup$
Ok, now I see your point, thx
$endgroup$
– kharandziuk
Dec 6 '18 at 12:53






$begingroup$
Ok, now I see your point, thx
$endgroup$
– kharandziuk
Dec 6 '18 at 12:53














$begingroup$
Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
$endgroup$
– guest271314
Jan 29 at 8:41






$begingroup$
Missing closing parenthesis ) at .then(). Why is second parameter to .then() or .catch() not used to handle error? Does the code at the question return the expected result?
$endgroup$
– guest271314
Jan 29 at 8:41












1 Answer
1






active

oldest

votes


















0












$begingroup$

I am not sure I understand why you are attempting to extend Writable here.



At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.



Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation






share|improve this answer









$endgroup$













  • $begingroup$
    Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
    $endgroup$
    – kharandziuk
    Jan 28 at 16:59













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f209148%2fconvert-a-stream-to-promise%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









0












$begingroup$

I am not sure I understand why you are attempting to extend Writable here.



At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.



Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation






share|improve this answer









$endgroup$













  • $begingroup$
    Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
    $endgroup$
    – kharandziuk
    Jan 28 at 16:59


















0












$begingroup$

I am not sure I understand why you are attempting to extend Writable here.



At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.



Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation






share|improve this answer









$endgroup$













  • $begingroup$
    Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
    $endgroup$
    – kharandziuk
    Jan 28 at 16:59
















0












0








0





$begingroup$

I am not sure I understand why you are attempting to extend Writable here.



At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.



Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation






share|improve this answer









$endgroup$



I am not sure I understand why you are attempting to extend Writable here.



At the end of the day, it seems as if your pipeline is what you are REALLY wanting to have a promise interface.



Luckily this is really easy to do with recent versions of node using Stream.pipeline and Util.promisify(). There is an even an example in the Stream.pipeline documentation







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 28 at 15:54









Mike BrantMike Brant

8,823622




8,823622












  • $begingroup$
    Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
    $endgroup$
    – kharandziuk
    Jan 28 at 16:59




















  • $begingroup$
    Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
    $endgroup$
    – kharandziuk
    Jan 28 at 16:59


















$begingroup$
Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
$endgroup$
– kharandziuk
Jan 28 at 16:59






$begingroup$
Why do I need it? for example testing - it's quite handy to have a result is promise for testing. Stream.pipeline - I don't have a 'collected' result with it
$endgroup$
– kharandziuk
Jan 28 at 16:59




















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


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%2fcodereview.stackexchange.com%2fquestions%2f209148%2fconvert-a-stream-to-promise%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

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...