{"id":1779,"date":"2017-04-13T17:30:02","date_gmt":"2017-04-13T15:30:02","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1779"},"modified":"2018-04-05T09:48:26","modified_gmt":"2018-04-05T07:48:26","slug":"resolve-promise-one-after-another-in-sequence","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2017\/04\/13\/resolve-promise-one-after-another-in-sequence\/","title":{"rendered":"Resolve Promise one after another, in sequence"},"content":{"rendered":"<p>Below is the code that is handy when you want to execute several Promise actions in sequence:<\/p>\n<pre class=\"brush:javascript\">\r\nfunction PromiseChain(arr, fct) {\r\n  var dfd = Promise.resolve();\r\n  var res = arr.map(function(item,idx) {\r\n    dfd = dfd.then(function() {\r\n      return fct(item,idx)\r\n    });\r\n    return dfd\r\n  });\r\n  return Promise.all(res)\r\n}\r\n\r\n\/\/ for short version:\r\n\/\/ function PromiseChain(n,r){var e=Promise.resolve(),i=n.map(function(n,i){return e=e.then(function(){return r(n,i)})});return Promise.all(i)}\r\n<\/pre>\n<p>And an example about how to use it:<\/p>\n<pre class=\"brush:javascript\">\r\nfunction promFunc(key) {\r\n  return new Promise(function(prom_res) {\r\n    setTimeout(function() { prom_res(key) }, 1000);\r\n  })\r\n}\r\n\r\nPromiseChain([\"a\", \"b\", \"c\"], function(val, idx) {\r\n  console.log(idx, val);\r\n  return promFunc(idx+\"_\"+val);\r\n})\r\n.then(function(res) {\r\n  console.log(res);\r\n})\r\n\r\n\/\/ result:\r\n\/\/ 0 a\r\n\/\/ 1 b\r\n\/\/ 2 c\r\n\/\/ Array [ \"0_a\", \"1_b\", \"2_c\" ]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is the code that is handy when you want to execute several Promise actions in sequence: function PromiseChain(arr, fct) { var dfd = Promise.resolve(); var res = arr.map(function(item,idx) { dfd = dfd.then(function() { return fct(item,idx) }); return dfd }); return Promise.all(res) } \/\/ for short version: \/\/ function PromiseChain(n,r){var e=Promise.resolve(),i=n.map(function(n,i){return e=e.then(function(){return r(n,i)})});return Promise.all(i)} And [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[170,13,33],"tags":[123,24,152,158],"class_list":["post-1779","post","type-post","status-publish","format-standard","hentry","category-english","category-niveau-intermediaire","category-programmation","tag-english","tag-javascript","tag-niveau-intermediaire","tag-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1779"}],"version-history":[{"count":7,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1779\/revisions"}],"predecessor-version":[{"id":1888,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1779\/revisions\/1888"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}