Repository navigation
how to change the value of variable after calling the function using tablesasjson? #25625
Description
Activity
emailis assigned in a callback function after table-to-JSON conversion is complete. If you try to use it just after thetabletojson.convertUrl()call it is still undefined because the mentioned callback will be executed asynchronously in a future loop cycle.See:
https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.
on Jan 21, 2019 @vsemozhetbyt the issue is that i need to use the result of the variable (email) after calling it in another function which is in another file.js therefore i need to grap the value of email and not to call the function. if i can to use return of the function as a variable its ok as well
If I understand correctly, you can only call the
file.jsfunction from the callback if you need to useemailfrom the result of the conversion.However, it seems
tabletojsonprovides a Promise API, so you can use async/await to make the code simpler.Something like this:
'use strict'; const tabletojson = require('tabletojson'); (async function main() { try { const url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes'; const array = await tabletojson.convertUrl(url); console.log(array[1][0]); } catch (err) { console.error(err); } })();
Reacted by Alex1233456Reacted by Alex1233456Reacted by Alex1233456General questions like this should be posted to the nodejs/help repo instead.
Reacted by Alex1233456, sovanndyul and Ben Noordhuis@vsemozhetbyt thanks for this awesome solution. can i ask the last question before closing the issue please?
while trying to exports the main function and its result like this: module.exports.main = main;
module.exports.result = result; into another file.js like this var test3 = require('./test3')
test3.main();
console.log(test3.result)
its appearing that module.exports.main = main;
^ReferenceError: main is not defined--- why that?
this is the new code (your edited code): 'use strict'; const tabletojson = require('tabletojson'); var result; ( async function main() { try { const url = 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes'; const array = await tabletojson.convertUrl(url); result = array[1][0]; } catch (err) { console.error(err); } })(); module.exports.main = main; module.exports.result = result; console.log(result)In my code, function
mainis not declared as a variable, it is used as named IIFE. That is whyReferenceError: main is not defined.If you need to use this code as exported function from a module, you need something llike this:
test-module.js:'use strict'; const tabletojson = require('tabletojson'); async function getTableAsArray(url) { try { return await tabletojson.convertUrl(url); } catch (err) { console.error(err); } } module.exports = { getTableAsArray, };
test.js:'use strict'; const testModule = require('./test-module.js'); (async function main() { try { const array = await testModule.getTableAsArray( 'https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes' ); console.log(array[1][0]); } catch (err) { console.error(err); } })();
I'm closing this. Please move it to nodejs/help if follow-up is necessary.
- addedwrong repoIssues that should be opened in another repository.Issues that should be opened in another repository.
on Jan 22, 2019
why the variable cannot be changed after calling the function?
this is my code: