-4

I am trying to create a program that analyses some data. The data I am collection form the internet with node.js. I'm having a very hard time with the fact that the code is asynchronous, and I'm getting a lot of bugs because of it.

I know that it is not efficient at all, but is there a way to write the whole code shynchronously?

*I've used a lot of promises and patched a lot of bugs, but I still keep getting bugs because the code is asynchronous, and I'm tired of thinking of creative ideas to make my code work.

Yuval Buium
  • 109
  • 1
  • 1
  • 4
  • Most async functions have APIs so that you can pass callbacks to be called. Did you try this? – Shriharsha KL Dec 09 '17 at 17:19
  • 1
    You can have a look at `async`/`await` constructs which are supported since nodejs 7.6 (https://www.infoq.com/news/2017/02/node-76-async-await). Your code will look like synchronous, and asynchronous functions, if called with `await` prefix, will effectively pause the caller execution until it finishes. – Andrew Starostin Dec 09 '17 at 17:24
  • 1
    have you considered that maybe Javascript is a bad language of choice here? Python or PHP might be a better fit – hanshenrik Dec 09 '17 at 17:24
  • 2
    @AndrewStarostin Also `async`/`await` does not turn Node.js into a synchronous platform, it just *looks* synchronous. You still have to understand the underlying asynchronous concepts. – Golo Roden Dec 09 '17 at 17:24
  • If you get data from the internet then its best to use promises, there may be a way of blocking xmlhttprequest (not sure if by now it's completely removed), it won't block your browser in node app but your code may run a lot slower. If you understand why promises are used and how code you write is non blocking (any function returns a value immediately) then it does get easier. Remember that `async` does not run on a new thread and `await` functions will return a promise immediately they don't wait for anything. https://stackoverflow.com/a/47678417/1641941 video link is very good. – HMR Dec 09 '17 at 18:29
  • I don't know why people would vote to close this as "primarily opinion based". What you can and can't write in node.js synchronously is not opinion at all - it's pure fact. I voted to reopen as there are two factual answers to the question already. – jfriend00 Dec 09 '17 at 21:41

2 Answers2

3

This highly depends on the functions you use. Of course you can write an entire Node.js program purely synchronous - you just have to avoid using any function that is asynchronous.

But as Node.js is asynchronous by heart, I doubt that this will work for more complex things. Since you mentioned loading data from over the network: Just this is already an example of a task that can't be accomplished synchronously in Node.js, as any I/O in Node.js is always asynchronous (except for the file system methods that exist twice, but they are the exception to the rule).

I think the way better approach to solve your problems is to either use a different platform than Node.js, or to appropriately learn to deal with asynchronous code.

I guess that this is not the answer you wanted to hear, but unfortunately that's just the way it is IMHO.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
0

If you're using networking (which it sounds like you are to retrieve data from other sources), then NO you can't write synchronous networking code in node.js. Does not exist. All networking API in node.js are asynchronous.

To use node.js networking, you must learn asynchronous programming.

It is possible to use only synchronous file I/O which should never be done in server request processing (because it wrecks scalability), but is sometimes useful in server startup and in non-server type scripts.

jfriend00
  • 683,504
  • 96
  • 985
  • 979