{"id":1850,"date":"2012-11-27T14:08:22","date_gmt":"2012-11-27T05:08:22","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=1850"},"modified":"2014-08-21T08:49:13","modified_gmt":"2014-08-20T23:49:13","slug":"post-1850","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/1850","title":{"rendered":"\u3010node.js\u3011\u4e00\u5ea6\u306b\u5b9f\u884c\u3059\u308b\u975e\u540c\u671f\u51e6\u7406\u306e\u6570\u3092\u5236\u9650\u3057\u3066\u3001\u4f55\u56de\u304b\u306b\u5206\u3051\u3066\u51e6\u7406\u3059\u308b"},"content":{"rendered":"<p><a href=\"http:\/\/blog.mixu.net\/2011\/02\/02\/essential-node-js-patterns-and-snippets\/\">Essential Node.js patterns and snippets<\/a><\/p>\n<pre class=\"lang:js decode:true \" title=\"pile.js\" >exports.Pile = function() {\r\n   this.pile = [];\r\n   this.concurrency = 0;\r\n   this.done = null;\r\n   this.max_concurrency = 10;\r\n}\r\nexports.Pile.prototype = {\r\n  add: function(callback) {\r\n   this.pile.push(callback);\r\n  },\r\n  run: function(done, max_concurrency) {\r\n      this.done = done || this.done;\r\n      this.max_concurrency = max_concurrency || this.max_concurrency;\r\n      var target = this.pile.length;\r\n      var that = this;\r\n      var next = function() {\r\n         that.concurrency--;\r\n         (--target == 0 ? that.done() : that.run());\r\n      };\r\n      while(this.concurrency &lt; this.max_concurrency &amp;&amp; this.pile.length &gt; 0) {\r\n         this.concurrency++;\r\n         var callback = this.pile.shift();\r\n         callback(next);\r\n      }\r\n   }\r\n};<\/pre>\n<p>\u4f7f\u7528\u4f8b\uff09 <\/p>\n<pre class=\"lang:js decode:true \" title=\"test.js\" >var pile = require(&quot;.\/pile.js&quot;).Pile;\r\nvar pilex = new pile();\r\n\r\nvar counter = 0;\r\n \r\nfor(var i = 0; i &lt; 50; i++) {\r\n   pilex.add( function test(next) {\u3000\u3000\/\/\u5b9f\u884c\u3059\u308b\u975e\u540c\u671f\u51e6\u7406\u3092\u767b\u9332\r\n      setTimeout( function() {\r\n         counter++;\r\n         console.log(counter +&quot; Hello world&quot;);\r\n         next();\r\n      }, 1500);\r\n     }\r\n   );\r\n}\r\npilex.run(function() {  \/\/\u767b\u9332\u3057\u305f\u51e6\u7406\u3092\u5b9f\u884c\r\n\tconsole.log(&quot;Done &quot;+counter);\r\n\t}, 5); \/\/\u4e00\u5ea6\u306b\u5b9f\u884c\u3059\u308b\u975e\u540c\u671f\u51e6\u7406\u306e\u5236\u9650\u6570<\/pre>\n<p>\u30b5\u30f3\u30d7\u30eb<br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/jsdo.it\/blogparts\/35dh\/js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Essential Node.js pattern&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-1850","post","type-post","status-publish","format-standard","hentry","category-javascript","category-node-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=1850"}],"version-history":[{"count":8,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1850\/revisions"}],"predecessor-version":[{"id":3266,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1850\/revisions\/3266"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=1850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=1850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=1850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}