{"id":5989,"date":"2014-04-11T16:52:08","date_gmt":"2014-04-11T16:52:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/while-loop-with-promises-collection-of-common-programming-errors-2\/"},"modified":"2014-04-11T16:52:08","modified_gmt":"2014-04-11T16:52:08","slug":"while-loop-with-promises-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/while-loop-with-promises-collection-of-common-programming-errors-2\/","title":{"rendered":"While loop with promises-Collection of common programming errors"},"content":{"rendered":"<p>Here&#8217;s a reusable function that I think is pretty clear.<\/p>\n<pre><code>var Q = require(\"q\");\n\n\/\/ `condition` is a function that returns a boolean\n\/\/ `body` is a function that returns a promise\n\/\/ returns a promise for the completion of the loop\nfunction promiseWhile(condition, body) {\n    var done = Q.defer();\n\n    function loop() {\n        \/\/ When the result of calling `condition` is no longer true, we are\n        \/\/ done.\n        if (!condition()) return done.resolve();\n        \/\/ Use `when`, in case `body` does not return a promise.\n        \/\/ When it completes loop again otherwise, if it fails, reject the\n        \/\/ done promise\n        Q.when(body(), loop, done.reject);\n    }\n\n    \/\/ Start running the loop in the next tick so that this function is\n    \/\/ completely async. It would be unexpected if `body` was called\n    \/\/ synchronously the first time.\n    Q.nextTick(loop);\n\n    \/\/ The promise\n    return done.promise;\n}\n\n\n\/\/ Usage\nvar index = 1;\npromiseWhile(function () { return index<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a reusable function that I think is pretty clear. var Q = require(&#8220;q&#8221;); \/\/ `condition` is a function that returns a boolean \/\/ `body` is a function that returns a promise \/\/ returns a promise for the completion of the loop function promiseWhile(condition, body) { var done = Q.defer(); function loop() { \/\/ When [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5989","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5989"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5989\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}