How to make sure all lines are executed before function return











up vote
0
down vote

favorite
1












Calling the readCSV from index.js



const productIds = await readCSV();


in another file:



async function readCSV() {
const filepath = path.resolve('src/input_csv.csv');
const readstream = await fs.createReadStream(filepath);
const stream = await readstream.pipe(parser());
let productIds = ;
await stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
await stream.on('end', () => {
console.log(productIds);
});
if (productIds.length > 0) return productIds;
else return 'no products found';
});
}


it is giving the output:



in main: 
SourceProductId 1000050429
SourceProductId 1132353
SourceProductId 999915195
SourceProductId 50162873
SourceProductId 1000661087
[ '1000050429', '1132353', '999915195', '50162873', '1000661087' ]


I'm expecting the function to return an array of all values read from CSV. but the seems like it is returning before executing the stream.on statement. How to make sure all lines are executed before returning. I placed await before every statement but no luck










share|improve this question


















  • 1




    async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
    – Yury Tarabanko
    Nov 20 at 12:59












  • how to make sure stream done before returning?
    – m9m9m
    Nov 20 at 13:01










  • I have added those as part of debugging now I have removed those.
    – m9m9m
    Nov 20 at 13:04










  • You need to create a Promise that will be resolved on stream end.
    – Yury Tarabanko
    Nov 20 at 13:04















up vote
0
down vote

favorite
1












Calling the readCSV from index.js



const productIds = await readCSV();


in another file:



async function readCSV() {
const filepath = path.resolve('src/input_csv.csv');
const readstream = await fs.createReadStream(filepath);
const stream = await readstream.pipe(parser());
let productIds = ;
await stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
await stream.on('end', () => {
console.log(productIds);
});
if (productIds.length > 0) return productIds;
else return 'no products found';
});
}


it is giving the output:



in main: 
SourceProductId 1000050429
SourceProductId 1132353
SourceProductId 999915195
SourceProductId 50162873
SourceProductId 1000661087
[ '1000050429', '1132353', '999915195', '50162873', '1000661087' ]


I'm expecting the function to return an array of all values read from CSV. but the seems like it is returning before executing the stream.on statement. How to make sure all lines are executed before returning. I placed await before every statement but no luck










share|improve this question


















  • 1




    async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
    – Yury Tarabanko
    Nov 20 at 12:59












  • how to make sure stream done before returning?
    – m9m9m
    Nov 20 at 13:01










  • I have added those as part of debugging now I have removed those.
    – m9m9m
    Nov 20 at 13:04










  • You need to create a Promise that will be resolved on stream end.
    – Yury Tarabanko
    Nov 20 at 13:04













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Calling the readCSV from index.js



const productIds = await readCSV();


in another file:



async function readCSV() {
const filepath = path.resolve('src/input_csv.csv');
const readstream = await fs.createReadStream(filepath);
const stream = await readstream.pipe(parser());
let productIds = ;
await stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
await stream.on('end', () => {
console.log(productIds);
});
if (productIds.length > 0) return productIds;
else return 'no products found';
});
}


it is giving the output:



in main: 
SourceProductId 1000050429
SourceProductId 1132353
SourceProductId 999915195
SourceProductId 50162873
SourceProductId 1000661087
[ '1000050429', '1132353', '999915195', '50162873', '1000661087' ]


I'm expecting the function to return an array of all values read from CSV. but the seems like it is returning before executing the stream.on statement. How to make sure all lines are executed before returning. I placed await before every statement but no luck










share|improve this question













Calling the readCSV from index.js



const productIds = await readCSV();


in another file:



async function readCSV() {
const filepath = path.resolve('src/input_csv.csv');
const readstream = await fs.createReadStream(filepath);
const stream = await readstream.pipe(parser());
let productIds = ;
await stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
await stream.on('end', () => {
console.log(productIds);
});
if (productIds.length > 0) return productIds;
else return 'no products found';
});
}


it is giving the output:



in main: 
SourceProductId 1000050429
SourceProductId 1132353
SourceProductId 999915195
SourceProductId 50162873
SourceProductId 1000661087
[ '1000050429', '1132353', '999915195', '50162873', '1000661087' ]


I'm expecting the function to return an array of all values read from CSV. but the seems like it is returning before executing the stream.on statement. How to make sure all lines are executed before returning. I placed await before every statement but no luck







node.js asynchronous async-await






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 12:53









m9m9m

437512




437512








  • 1




    async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
    – Yury Tarabanko
    Nov 20 at 12:59












  • how to make sure stream done before returning?
    – m9m9m
    Nov 20 at 13:01










  • I have added those as part of debugging now I have removed those.
    – m9m9m
    Nov 20 at 13:04










  • You need to create a Promise that will be resolved on stream end.
    – Yury Tarabanko
    Nov 20 at 13:04














  • 1




    async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
    – Yury Tarabanko
    Nov 20 at 12:59












  • how to make sure stream done before returning?
    – m9m9m
    Nov 20 at 13:01










  • I have added those as part of debugging now I have removed those.
    – m9m9m
    Nov 20 at 13:04










  • You need to create a Promise that will be resolved on stream end.
    – Yury Tarabanko
    Nov 20 at 13:04








1




1




async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
– Yury Tarabanko
Nov 20 at 12:59






async/await only works with Promise-like objects. Streams are not like that. You need to learn about Promises before scattering await keyword all over your code. For example await fs.createReadStream(filepath) what are you trying to "await" for here?
– Yury Tarabanko
Nov 20 at 12:59














how to make sure stream done before returning?
– m9m9m
Nov 20 at 13:01




how to make sure stream done before returning?
– m9m9m
Nov 20 at 13:01












I have added those as part of debugging now I have removed those.
– m9m9m
Nov 20 at 13:04




I have added those as part of debugging now I have removed those.
– m9m9m
Nov 20 at 13:04












You need to create a Promise that will be resolved on stream end.
– Yury Tarabanko
Nov 20 at 13:04




You need to create a Promise that will be resolved on stream end.
– Yury Tarabanko
Nov 20 at 13:04












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










stream.on doesn't return a promise, so you cannot await it.



You can work around this by returning a promise from your readCSV function:



function readCSV() {
return new Promise(resolve => {
const filepath = path.resolve('src/input_csv.csv');
const readstream = fs.createReadStream(filepath);
const stream = readstream.pipe(parser());
let productIds = ;
stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
stream.on('end', () => {
console.log(productIds);
if (productIds.length > 0) resolve(productIds);
else resolve('no products found');
});

});
}


You create a promise by passing a callback function as argument. That callback function gets an argument resolve, which is a another callback function that you call when your asynchronous operation is done, passing the result.



In the example above, we call this resolve callback with the product IDs after the file read stream has finished.



Since readCSV now returns a promise, you can await it like you did in your code example:



const productIds = await readCSV();





share|improve this answer























  • Sorry misread your answer.
    – Yury Tarabanko
    Nov 20 at 13:03










  • It worked!! please explain.
    – m9m9m
    Nov 22 at 9:50










  • I've added some explanation to make it clear what's going on
    – Patrick Hund
    Nov 22 at 10:29











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',
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%2f53393442%2fhow-to-make-sure-all-lines-are-executed-before-function-return%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








up vote
2
down vote



accepted










stream.on doesn't return a promise, so you cannot await it.



You can work around this by returning a promise from your readCSV function:



function readCSV() {
return new Promise(resolve => {
const filepath = path.resolve('src/input_csv.csv');
const readstream = fs.createReadStream(filepath);
const stream = readstream.pipe(parser());
let productIds = ;
stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
stream.on('end', () => {
console.log(productIds);
if (productIds.length > 0) resolve(productIds);
else resolve('no products found');
});

});
}


You create a promise by passing a callback function as argument. That callback function gets an argument resolve, which is a another callback function that you call when your asynchronous operation is done, passing the result.



In the example above, we call this resolve callback with the product IDs after the file read stream has finished.



Since readCSV now returns a promise, you can await it like you did in your code example:



const productIds = await readCSV();





share|improve this answer























  • Sorry misread your answer.
    – Yury Tarabanko
    Nov 20 at 13:03










  • It worked!! please explain.
    – m9m9m
    Nov 22 at 9:50










  • I've added some explanation to make it clear what's going on
    – Patrick Hund
    Nov 22 at 10:29















up vote
2
down vote



accepted










stream.on doesn't return a promise, so you cannot await it.



You can work around this by returning a promise from your readCSV function:



function readCSV() {
return new Promise(resolve => {
const filepath = path.resolve('src/input_csv.csv');
const readstream = fs.createReadStream(filepath);
const stream = readstream.pipe(parser());
let productIds = ;
stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
stream.on('end', () => {
console.log(productIds);
if (productIds.length > 0) resolve(productIds);
else resolve('no products found');
});

});
}


You create a promise by passing a callback function as argument. That callback function gets an argument resolve, which is a another callback function that you call when your asynchronous operation is done, passing the result.



In the example above, we call this resolve callback with the product IDs after the file read stream has finished.



Since readCSV now returns a promise, you can await it like you did in your code example:



const productIds = await readCSV();





share|improve this answer























  • Sorry misread your answer.
    – Yury Tarabanko
    Nov 20 at 13:03










  • It worked!! please explain.
    – m9m9m
    Nov 22 at 9:50










  • I've added some explanation to make it clear what's going on
    – Patrick Hund
    Nov 22 at 10:29













up vote
2
down vote



accepted







up vote
2
down vote



accepted






stream.on doesn't return a promise, so you cannot await it.



You can work around this by returning a promise from your readCSV function:



function readCSV() {
return new Promise(resolve => {
const filepath = path.resolve('src/input_csv.csv');
const readstream = fs.createReadStream(filepath);
const stream = readstream.pipe(parser());
let productIds = ;
stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
stream.on('end', () => {
console.log(productIds);
if (productIds.length > 0) resolve(productIds);
else resolve('no products found');
});

});
}


You create a promise by passing a callback function as argument. That callback function gets an argument resolve, which is a another callback function that you call when your asynchronous operation is done, passing the result.



In the example above, we call this resolve callback with the product IDs after the file read stream has finished.



Since readCSV now returns a promise, you can await it like you did in your code example:



const productIds = await readCSV();





share|improve this answer














stream.on doesn't return a promise, so you cannot await it.



You can work around this by returning a promise from your readCSV function:



function readCSV() {
return new Promise(resolve => {
const filepath = path.resolve('src/input_csv.csv');
const readstream = fs.createReadStream(filepath);
const stream = readstream.pipe(parser());
let productIds = ;
stream.on('data', data => {
productIds.push(data.SourceProductId);
console.log('SourceProductId', data.SourceProductId);
});
stream.on('end', () => {
console.log(productIds);
if (productIds.length > 0) resolve(productIds);
else resolve('no products found');
});

});
}


You create a promise by passing a callback function as argument. That callback function gets an argument resolve, which is a another callback function that you call when your asynchronous operation is done, passing the result.



In the example above, we call this resolve callback with the product IDs after the file read stream has finished.



Since readCSV now returns a promise, you can await it like you did in your code example:



const productIds = await readCSV();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 10:28

























answered Nov 20 at 13:01









Patrick Hund

6,79252147




6,79252147












  • Sorry misread your answer.
    – Yury Tarabanko
    Nov 20 at 13:03










  • It worked!! please explain.
    – m9m9m
    Nov 22 at 9:50










  • I've added some explanation to make it clear what's going on
    – Patrick Hund
    Nov 22 at 10:29


















  • Sorry misread your answer.
    – Yury Tarabanko
    Nov 20 at 13:03










  • It worked!! please explain.
    – m9m9m
    Nov 22 at 9:50










  • I've added some explanation to make it clear what's going on
    – Patrick Hund
    Nov 22 at 10:29
















Sorry misread your answer.
– Yury Tarabanko
Nov 20 at 13:03




Sorry misread your answer.
– Yury Tarabanko
Nov 20 at 13:03












It worked!! please explain.
– m9m9m
Nov 22 at 9:50




It worked!! please explain.
– m9m9m
Nov 22 at 9:50












I've added some explanation to make it clear what's going on
– Patrick Hund
Nov 22 at 10:29




I've added some explanation to make it clear what's going on
– Patrick Hund
Nov 22 at 10:29


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53393442%2fhow-to-make-sure-all-lines-are-executed-before-function-return%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

Wiesbaden

Marschland

Dieringhausen