how to share variables inside and outside of async functions?

arslan

I am having trouble with async in nodejs. In the following code

//Imagine we are inside a function
// many things here before reading a file
 this.data_receiver;    //want to this get file content
 fs.readFile('/data.txt', (err, data) => {
   if (err) throw err;
    //console.log(data);
     this.data_receiver= data;  // get data
});

//I want to process data_receiver
......

The problem is that how can I get data little by little from a file and store it into some variables, then use that variable outside?

I am new to this async, having trouble.

unional

The this is not bounded to your function inside the callback. You should do:

this.data_receiver;    //want to this get file content
var me = this;
fs.readFile('/data.txt', (err, data) => {
  if (err) throw err;
  //console.log(data);
  me.data_receiver = data;  // get data
});
console.log(this.data_receiver);

Also, this is about callback scope. While technically saying, it is "async functions", but it is more common to associate "async functions" to async functions, such as:

async function foo() {
  return Promise.resolve();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How node.js save variables outside an async operation for accessing inside it's callback?

From Dev

What's the difference between declaring variables inside function and outside functions

From Dev

How to use python functions and variables inside XSLT?

From Dev

how to documents variables inside functions via phpDoc

From Dev

How to access and edit variables inside functions in python

From Dev

how to documents variables inside functions via phpDoc

From Dev

R Shiny - how to share variables between Rendering functions?

From Dev

How do I share variables inside a singleton class

From Dev

How do I share variables inside a singleton class

From Dev

How to get variables from the outside, inside a function in jQuery?

From Dev

How to set variables inside a foreach loop for outside access in PHP?

From Dev

Share variables internally across functions

From Dev

Share variables between functions in Swift

From Dev

Share variables between functions in Swift

From Dev

Share variables internally across functions

From Dev

Using local variables outside their functions

From Dev

Declare variables outside of Erlang functions

From Dev

Using local variables outside their functions

From Dev

Declare variables outside of Erlang functions

From Dev

Accessing outside variables inside the 'then' function

From Dev

Variables inside and outside of object methods

From Dev

How to call function after completion of async functions inside loop?

From Dev

How to call function after completion of async functions inside loop?

From Dev

How does nesting async functions inside the final callback work?

From Dev

How do I throw exceptions inside async functions?

From Dev

How to share variables with siblings?

From Dev

How to Pass variables inside functions using new method

From Dev

Python global list modification inside and outside of functions

From Dev

Should functions be inside jQuery's extend, or outside?

Related Related

  1. 1

    How node.js save variables outside an async operation for accessing inside it's callback?

  2. 2

    What's the difference between declaring variables inside function and outside functions

  3. 3

    How to use python functions and variables inside XSLT?

  4. 4

    how to documents variables inside functions via phpDoc

  5. 5

    How to access and edit variables inside functions in python

  6. 6

    how to documents variables inside functions via phpDoc

  7. 7

    R Shiny - how to share variables between Rendering functions?

  8. 8

    How do I share variables inside a singleton class

  9. 9

    How do I share variables inside a singleton class

  10. 10

    How to get variables from the outside, inside a function in jQuery?

  11. 11

    How to set variables inside a foreach loop for outside access in PHP?

  12. 12

    Share variables internally across functions

  13. 13

    Share variables between functions in Swift

  14. 14

    Share variables between functions in Swift

  15. 15

    Share variables internally across functions

  16. 16

    Using local variables outside their functions

  17. 17

    Declare variables outside of Erlang functions

  18. 18

    Using local variables outside their functions

  19. 19

    Declare variables outside of Erlang functions

  20. 20

    Accessing outside variables inside the 'then' function

  21. 21

    Variables inside and outside of object methods

  22. 22

    How to call function after completion of async functions inside loop?

  23. 23

    How to call function after completion of async functions inside loop?

  24. 24

    How does nesting async functions inside the final callback work?

  25. 25

    How do I throw exceptions inside async functions?

  26. 26

    How to share variables with siblings?

  27. 27

    How to Pass variables inside functions using new method

  28. 28

    Python global list modification inside and outside of functions

  29. 29

    Should functions be inside jQuery's extend, or outside?

HotTag

Archive