{
"$type": "site.standard.document",
"content": "Javascript is a single-threaded language, this in certain situations can be very limiting because the process is stuck executing on one thread and can't fully utilize the CPU it is running on but thanks to concurrency, it's single-threaded nature is less of a problem.\r\n\r\n***But wait, what is concurrency !?***\r\n\r\nI am glad you asked (_Even if you didn't ask just pretend you did and we will move on_ 😉)\r\n\r\n### Basics\r\n\r\nConcurrency means two or more processes running together in one thread but not at the same time, a lot of us have come across concurrency in Node JS but might not have noticed it (_Prime example = me_ 😅).\r\n\r\nExample:\r\n\r\nYou can run this code!!\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nfs.writeFile('./file.txt', 'Hello World!!', function(){\r\n console.log('Wrote \"Hello World!!\" into file.txt');\r\n});\r\n\r\nconsole.log('Writing \"Hello World!!\" into file.txt');\r\n\r\n{% endrunkit %}\r\n\r\nThe code in the example above must be familiar to most of us, but did you know this is a prime example of concurrency?. We all agree that line 7 is executed before line 5 right, __Well that is concurrency!__, multiple separate processes running in the same thread by taking turns to execute code. \r\n\r\nThese are the steps taken during execution.\r\n\r\n- fs.writeFile calls an underlying function which acts as a proxy between JS and C++\r\n\r\n- The function calls C++ code which creates a process on the event loop that will handle the write operation\r\n\r\n- console.log('Writing \"Hello World!!\" into file.txt')\r\n\r\n- The process writes content to `file.txt`\r\n\r\n- The process returns and our callback is executed\r\n\r\n- console.log('Wrote \"Hello World!!\" into file.txt')\r\n\r\nThis is great and all but there is one side effect to writing code with concurrent behavior and it is affectionately called the **\"Callback Hell\"**\r\n\r\nExample:\r\n\r\nWriting a file and then reading from it.\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nfs.writeFile('./file.txt', 'Hello World!!', function(){\r\n console.log('Wrote \"Hello World!!\" into file.txt');\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n throw new Error(err);\r\n }\r\n console.log('Read \"', data.toString(), '\" from ./file.txt')\r\n })\r\n\r\n});\r\n\r\nconsole.log('Writing \"Hello World!!\" into file.txt');\r\n{% endrunkit %}\r\n\r\nThis gets exponentially worse the more you need to use data provided by such a function but the entire ordeal can be avoided when you use **Promises**.\r\n\r\n### Promises\r\n\r\nPromises are javascript structures that \"Promise\" the resolution/failure of asynchronous code and help us handle their successes/failures in a **syntactically** synchronous manner.\r\n\r\nExample:\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nconst readPromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve(data);\r\n })\r\n });\r\n}\r\n\r\nconst writePromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.writeFile('./file.txt', 'Hello world!!', function(err){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve();\r\n })\r\n });\r\n}\r\n\r\nwritePromise()\r\n.then(() => {\r\n return readPromise()\r\n})\r\n.then(data => \r\n console.log(data.toString()))\r\n.catch(err => console.log(err));\r\n\r\n{% endrunkit %}\r\n\r\nThe above code does not look that much better but with promises also come along the async/await keywords which will be extra helpful in cleaning up our code.\r\n\r\nThe `await` keyword helps us retrieve data resolved by a promise as if it were directly returned from a synchronous function, but `await` only works from within an asynchronous function and this is where the `async` keyword comes in, it helps us define asynchronous functions where we can use `await`.\r\n\r\nExample:\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nconst readPromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve(data);\r\n })\r\n });\r\n}\r\n\r\nconst writePromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.writeFile('./file.txt', 'Hello world!!', function(err){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve();\r\n })\r\n });\r\n}\r\n\r\nasync function start(){\r\n await writePromise();\r\n // `data` returned as if it were from a synchronous function\r\n const data = await readPromise();\r\n console.log(data.toString());\r\n};\r\n\r\nstart()\r\n\r\n{% endrunkit %}\r\n\r\nNow that is clean asynchronous code!!\r\n\r\n### Taking it further\r\n\r\nNow that we can create promises and `await` them, we no longer need to use callbacks. Here are some general examples.\r\n\r\n__Note: The default libraries in Node JS don't have great support for promises so we will be using third-party libraries for the async examples__\r\n\r\n#### API calls\r\n\r\nUsing callbacks\r\n\r\n{% runkit %}\r\n\r\nconst http = require('http');\r\n\r\nhttp.request('http://jsonplaceholder.typicode.com/todos/1', function(res) {\r\n let data = '';\r\n res.setEncoding('utf8');\r\n res.on('data', function (chunk) {\r\n data += chunk;\r\n });\r\n res.on('end', function(){\r\n console.log(JSON.parse(data));\r\n })\r\n\r\n}).end();\r\n\r\n{% endrunkit %}\r\n\r\nUsing promises\r\n\r\n{% runkit %}\r\n\r\nconst fetch = require('node-fetch');\r\n\r\nasync function start(){\r\n const response = await fetch('http://jsonplaceholder.typicode.com/todos/1');\r\n const data = await response.text();\r\n console.log(JSON.parse(data));\r\n}\r\n\r\nstart();\r\n\r\n{% endrunkit %}\r\n\r\n#### Spawn processes\r\n\r\nUsing callbacks\r\n\r\n{% runkit %}\r\n\r\nconst { spawn } = require('child_process');\r\nconst ls = spawn('echo', ['Hello World!!']);\r\nlet data = '';\r\nls.stdout.on('data', (_data) => {\r\n data += _data;\r\n});\r\n\r\nls.on('close', (code) => {\r\n console.log(data);\r\n});\r\n\r\n{% endrunkit %}\r\n\r\nUsing promises\r\n\r\n{% runkit %}\r\n\r\nconst spawn = require('spawn-promise');\r\n\r\nasync function start(){\r\n const out = await spawn('echo',['Hello World!!']);\r\n console.log(out.toString());\r\n}\r\n\r\nstart();\r\n\r\n{% endrunkit %}\r\n\r\n## Conclusion\r\n\r\nConcurrency is a beautiful thing, especially in large scale applications where speed is a huge priority and I hope this post helped you learn a bit more about it and how best to apply it.\r\n\r\n## Thanks for reading!!!\r\n\r\n*Consider giving me a follow on [Twitter](https://twitter.com/neutrino2211) and you can check out my previous post [here](https://dev.to/neutrino2211/how-to-avoid-javascript-bugs-31pm)*",
"description": "A look at concurrency in nodejs and how to use it",
"path": "/posts/what-is-concurrency-in-node-js-3lgo",
"publishedAt": "2019-04-28T17:33:04.000Z",
"site": "https://blog.mainasara.dev",
"tags": [
"webdev",
"javascript",
"showdev",
"nodejs"
],
"textContent": "Javascript is a single-threaded language, this in certain situations can be very limiting because the process is stuck executing on one thread and can't fully utilize the CPU it is running on but thanks to concurrency, it's single-threaded nature is less of a problem.\r\n\r\n***But wait, what is concurrency !?***\r\n\r\nI am glad you asked (_Even if you didn't ask just pretend you did and we will move on_ 😉)\r\n\r\n### Basics\r\n\r\nConcurrency means two or more processes running together in one thread but not at the same time, a lot of us have come across concurrency in Node JS but might not have noticed it (_Prime example = me_ 😅).\r\n\r\nExample:\r\n\r\nYou can run this code!!\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nfs.writeFile('./file.txt', 'Hello World!!', function(){\r\n console.log('Wrote \"Hello World!!\" into file.txt');\r\n});\r\n\r\nconsole.log('Writing \"Hello World!!\" into file.txt');\r\n\r\n{% endrunkit %}\r\n\r\nThe code in the example above must be familiar to most of us, but did you know this is a prime example of concurrency?. We all agree that line 7 is executed before line 5 right, __Well that is concurrency!__, multiple separate processes running in the same thread by taking turns to execute code. \r\n\r\nThese are the steps taken during execution.\r\n\r\n- fs.writeFile calls an underlying function which acts as a proxy between JS and C++\r\n\r\n- The function calls C++ code which creates a process on the event loop that will handle the write operation\r\n\r\n- console.log('Writing \"Hello World!!\" into file.txt')\r\n\r\n- The process writes content to `file.txt`\r\n\r\n- The process returns and our callback is executed\r\n\r\n- console.log('Wrote \"Hello World!!\" into file.txt')\r\n\r\nThis is great and all but there is one side effect to writing code with concurrent behavior and it is affectionately called the **\"Callback Hell\"**\r\n\r\nExample:\r\n\r\nWriting a file and then reading from it.\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nfs.writeFile('./file.txt', 'Hello World!!', function(){\r\n console.log('Wrote \"Hello World!!\" into file.txt');\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n throw new Error(err);\r\n }\r\n console.log('Read \"', data.toString(), '\" from ./file.txt')\r\n })\r\n\r\n});\r\n\r\nconsole.log('Writing \"Hello World!!\" into file.txt');\r\n{% endrunkit %}\r\n\r\nThis gets exponentially worse the more you need to use data provided by such a function but the entire ordeal can be avoided when you use **Promises**.\r\n\r\n### Promises\r\n\r\nPromises are javascript structures that \"Promise\" the resolution/failure of asynchronous code and help us handle their successes/failures in a **syntactically** synchronous manner.\r\n\r\nExample:\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nconst readPromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve(data);\r\n })\r\n });\r\n}\r\n\r\nconst writePromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.writeFile('./file.txt', 'Hello world!!', function(err){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve();\r\n })\r\n });\r\n}\r\n\r\nwritePromise()\r\n.then(() => {\r\n return readPromise()\r\n})\r\n.then(data => \r\n console.log(data.toString()))\r\n.catch(err => console.log(err));\r\n\r\n{% endrunkit %}\r\n\r\nThe above code does not look that much better but with promises also come along the async/await keywords which will be extra helpful in cleaning up our code.\r\n\r\nThe `await` keyword helps us retrieve data resolved by a promise as if it were directly returned from a synchronous function, but `await` only works from within an asynchronous function and this is where the `async` keyword comes in, it helps us define asynchronous functions where we can use `await`.\r\n\r\nExample:\r\n\r\n{% runkit %}\r\n\r\nconst fs = require('fs');\r\n\r\nconst readPromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.readFile('./file.txt', function(err, data){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve(data);\r\n })\r\n });\r\n}\r\n\r\nconst writePromise = function(){\r\n return new Promise(function(resolve, reject){\r\n fs.writeFile('./file.txt', 'Hello world!!', function(err){\r\n if(err){\r\n reject(err);\r\n }\r\n resolve();\r\n })\r\n });\r\n}\r\n\r\nasync function start(){\r\n await writePromise();\r\n // `data` returned as if it were from a synchronous function\r\n const data = await readPromise();\r\n console.log(data.toString());\r\n};\r\n\r\nstart()\r\n\r\n{% endrunkit %}\r\n\r\nNow that is clean asynchronous code!!\r\n\r\n### Taking it further\r\n\r\nNow that we can create promises and `await` them, we no longer need to use callbacks. Here are some general examples.\r\n\r\n__Note: The default libraries in Node JS don't have great support for promises so we will be using third-party libraries for the async examples__\r\n\r\n#### API calls\r\n\r\nUsing callbacks\r\n\r\n{% runkit %}\r\n\r\nconst http = require('http');\r\n\r\nhttp.request('http://jsonplaceholder.typicode.com/todos/1', function(res) {\r\n let data = '';\r\n res.setEncoding('utf8');\r\n res.on('data', function (chunk) {\r\n data += chunk;\r\n });\r\n res.on('end', function(){\r\n console.log(JSON.parse(data));\r\n })\r\n\r\n}).end();\r\n\r\n{% endrunkit %}\r\n\r\nUsing promises\r\n\r\n{% runkit %}\r\n\r\nconst fetch = require('node-fetch');\r\n\r\nasync function start(){\r\n const response = await fetch('http://jsonplaceholder.typicode.com/todos/1');\r\n const data = await response.text();\r\n console.log(JSON.parse(data));\r\n}\r\n\r\nstart();\r\n\r\n{% endrunkit %}\r\n\r\n#### Spawn processes\r\n\r\nUsing callbacks\r\n\r\n{% runkit %}\r\n\r\nconst { spawn } = require('child_process');\r\nconst ls = spawn('echo', ['Hello World!!']);\r\nlet data = '';\r\nls.stdout.on('data', (_data) => {\r\n data += _data;\r\n});\r\n\r\nls.on('close', (code) => {\r\n console.log(data);\r\n});\r\n\r\n{% endrunkit %}\r\n\r\nUsing promises\r\n\r\n{% runkit %}\r\n\r\nconst spawn = require('spawn-promise');\r\n\r\nasync function start(){\r\n const out = await spawn('echo',['Hello World!!']);\r\n console.log(out.toString());\r\n}\r\n\r\nstart();\r\n\r\n{% endrunkit %}\r\n\r\n## Conclusion\r\n\r\nConcurrency is a beautiful thing, especially in large scale applications where speed is a huge priority and I hope this post helped you learn a bit more about it and how best to apply it.\r\n\r\n## Thanks for reading!!!\r\n\r\n*Consider giving me a follow on [Twitter](https://twitter.com/neutrino2211) and you can check out my previous post [here](https://dev.to/neutrino2211/how-to-avoid-javascript-bugs-31pm)*",
"title": "What Is Concurrency In Node JS?"
}