{"id":5106,"date":"2016-07-07T11:04:53","date_gmt":"2016-07-07T02:04:53","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=5106"},"modified":"2018-03-07T18:21:42","modified_gmt":"2018-03-07T09:21:42","slug":"post-5106","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3v4\/5106","title":{"rendered":"[D3.js]\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u3092promise\u3092\u4f7f\u3063\u3066\u7ba1\u7406\u3059\u308b\u3002"},"content":{"rendered":"<p>\u30bf\u30a4\u30c8\u30eb\u306e\u307e\u3093\u307e\u306e\u5185\u5bb9\u3067\u3059\u3002<\/p>\n<p>\u4ed5\u4e8b\u3067\u30c7\u30fc\u30bf\u30d3\u30b8\u30e5\u30a2\u30e9\u30a4\u30bc\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u3066\u3044\u308b\u3068\u3001\u300cA\u3068B\u306e\u8981\u7d20\u306e\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u304c\u5b8c\u4e86\u3057\u3066\u304b\u3089\u3001B\u306e\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u3092\u59cb\u3081\u308b\u300d\u306a\u3069\u8907\u96d1\u306a\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306b\u306a\u308b\u3053\u3068\u304c\u591a\u3005\u6709\u308a\u307e\u3059\u3002<br \/>\n\u8907\u6570\u306e\u8981\u7d20\u3092\u52d5\u304b\u3057\u3064\u3064\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u3092\u7e4b\u3050\u306e\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3067\u306f\u8907\u96d1\u306b\u306a\u308a\u3059\u304e\u3066\u7ba1\u7406\u304c\u96e3\u3057\u3044\u306e\u3067\u3001\u6700\u8fd1\u306fpromise\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002\u307e\u3060\u4f7f\u3044\u3053\u306a\u305b\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u4fbf\u5229\u306a\u306e\u3067\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u8f09\u305b\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/bl.ocks.org\/shimizu\/8998fb9899e65b3196d5690182b674cb\">example<\/a><\/p>\n<pre class=\"lang:js decode:true \" >const svg = d3.select('svg')\n\nconst circle = svg.append(\"circle\")\n  .attr(\"cx\", 100)\n  .attr(\"cy\", 50)\n  .attr(\"r\", 20)\n    \nconst rect = svg.append(\"rect\")\n  .attr(\"x\", 100)\n  .attr(\"y\", 100)\n  .attr(\"width\", 30)\n  .attr(\"height\", 30)\n\nconst line = svg.append(\"line\")\n  .attr(\"x1\", 100)\n  .attr(\"y1\", 150)\n  .attr(\"x2\", 100)\n  .attr(\"y2\", 150)\n  .attr(\"stroke\", \"black\")\n\n\/\/D3\u306e\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u3092promise\u3067\u5305\u3080\nconst p1 = () => {\n  return new Promise((resolve, reject) => {\n    circle.transition().duration(Math.floor(Math.random()*5000))\n      .attr(\"cx\", 800)\n      .on(\"end\", resolve) \n  })\n}\n\nconst p2 = () => {\n  return new Promise((resolve, reject) => {\n    rect.transition().duration(Math.floor(Math.random()*5000))\n      .attr(\"x\", 800)\n      .on(\"end\", resolve) \n  })\n}\n\nconst p3 = () => {\n  return new Promise((resolve, reject) => {\n    line.transition().duration(Math.floor(Math.random()*5000))\n      .attr(\"x2\", 800)\n      .on(\"end\", resolve) \n  })\n}\n\n\nd3.select(\"button\").on(\"click\", function(){\n  \/\/\u30dd\u30b8\u30b7\u30e7\u30f3\u3092\u30ea\u30bb\u30c3\u30c8\n  d3.select(\"body\").style(\"background-color\", \"white\")\n  circle.attr(\"cx\", 100)\n  rect.attr(\"x\", 100)\n  line.attr(\"x2\", 100)\n\n  \/\/\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u958b\u59cb\n  Promise.all([p1(), p2(), p3()]).then(endFn) \n})\n\n\/\/\u30c8\u30e9\u30f3\u30b8\u30b7\u30e7\u30f3\u304c\u7d42\u4e86\u3057\u305f\u3089\u80cc\u666f\u8272\u3092\u5909\u3048\u308b\nfunction endFn(){\n  d3.select(\"body\").style(\"background-color\", \"pink\")\n}   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30bf\u30a4\u30c8\u30eb\u306e\u307e\u3093\u307e\u306e\u5185\u5bb9\u3067\u3059\u3002 \u4ed5\u4e8b\u3067\u30c7\u30fc\u30bf\u30d3\u30b8\u30e5\u30a2&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-5106","post","type-post","status-publish","format-standard","hentry","category-d3v4"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/5106","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=5106"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/5106\/revisions"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=5106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=5106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=5106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}