-2

I am new to NodeJS and there is something I don't understand.

The basic feature of Node is asynchronous but JS itself does to (through setTimeout). So why features like Promise did not exist before Node ?

@EDIT: With the response from Christoph, I understand that the JS and Node today are similar, so both are asynchronous non blocking I/O ?

Thank you all.

Quoc-Hao Tran
  • 1,312
  • 3
  • 13
  • 20
  • how `setTimeout` works [in the browser](https://stackoverflow.com/a/29391634/2476755) versus [in node](https://stackoverflow.com/a/10767807/2476755) – royhowie Jul 17 '17 at 19:07
  • Try watching this short video: https://www.youtube.com/watch?v=8aGhZQkoFbQ It might explain a little biit more about what it means for nodejs. – K. Kirsz Jul 17 '17 at 19:09
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. – Quentin Jul 17 '17 at 19:28

1 Answers1

2

That the Promise API was just recently added to the javascript core has nothing to do with node. It's just part of the language development itself.

Javascript started out as a simple script language to manipulate the Browser DOM. It was developed in 95 by Brendan Eich within 2 weeks. This means, most of the features you have today, where not present. E.g. XMLHTTPRequest, the core of every modern website was not introduced until 2000. And at that time a lot of people still considered javascript a nice gimmick to create some flashy text effects, but not a serious tool to do (web) development.

After the dust of the browser war settled and javascript was triumphant over flash and java, vendors agreed that they should actually work together and there need to be strict web standards for all browsers to adhere to and the standardization of all web technologies (HTML,CSS,EcmaScript) gained a massive boost. With that, javascript gained a lot of new features.

With the success of javascript people started thinking "Why only use javascript for client side scripting?" - so in 2009 Ryan Dahl created Node.js, a JavaScript run-time environment for executing JavaScript code server-side.

This means, at their core "browser javascript" and "nodejs" both actually base on ECMAScript and run on the same principle of single threaded asynchronous execution. Both have the option to spawn worker threads. Basic functionality like setTimeout is pretty much the same in browsers and node.

However, the browsers have custom objects to take care of rendering the web page and interacting with the user and are very strictly sandboxed, while node has all the APIs necessary for a server side language (e.g. extensive file access) that would essentially pose security risks if they where present in a browser.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • I understand that, but did asynchronous behavior from JS exist before Node ? Is it inspired ? And why libraries like Bluebird was written in NodeJs ? Thank you for your response. – Quoc-Hao Tran Jul 17 '17 at 19:49
  • @Quoc-HaoTran NodeJS IS javascript, so all it's paradigms are the same like the browser's javascript. Bluebird is not written in NodeJS, it's a library FOR nodejs written in javascript. I edited my answer, I hope it is clearer now? – Christoph Jul 17 '17 at 20:38
  • Thank you very much, it is clearer now. Can I ask you one more question ? How can we write an async function in JS ? – Quoc-Hao Tran Jul 18 '17 at 17:40
  • Use the [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) keyword for this. The function then returns a Promise object. Be careful because IE does not support this! – Christoph Jul 18 '17 at 17:42
  • `async` is the new features from es7 so before it is there another way ? – Quoc-Hao Tran Jul 20 '17 at 09:23
  • @Quoc-HaoTran I'm not exactly sure what you mean with the question, but certain elements of the language are asynchronous like AJAX requests, timeouts and DOM event (and possible others as well). Also you can spawn worker threads, which give you the option of multi threading. – Christoph Jul 20 '17 at 14:01
  • I mean, before the language support `async`, did we use leverage like AJAX and timeouts to create an asynchronous function ? – Quoc-Hao Tran Jul 21 '17 at 15:00