JavaScript: Async Promise “while loop”

來源:互聯網
上載者:User

標籤:dia   cycle   chain   expected   ice   argument   next   otherwise   his   

This is a solution to the situation where you have an asynchronous task you want to perform over and over again, in a non-blocking fashion, stopping when some condition is met.

To set the stage, I’ll be using the Bluebird Promise library, the best Promise library I’ve used.

First, we’ll construct the Promise function which will do the looping:

var Promise = require(‘bluebird‘);var promiseWhile = function(condition, action) {    var resolver = Promise.defer();    var loop = function() {        if (!condition()) return resolver.resolve();        return Promise.cast(action())            .then(loop)            .catch(resolver.reject);    };    process.nextTick(loop);    return resolver.promise;};

It receives 2 arguments, both expected to be functions:

  1. condition - This is a predicate function which shall return true or false and indicate whether the end state has been reached. true if done, falseotherwise.
  2. action - This is the async task to be performed, which itself should return a Promise.

A couple things to note:

  • I simulated the async task with a simple setTimeout but this would be optimal for looping over Database calls, HTTP requests, or anything else requiring latency.
  • We use a function for the condition which allows us to do any manner of interesting tests for completion.
  • We utilize process.nextTick to wait until the next CPU cycle before starting. If we don’t do this, it will complete immediately and will not “loop.”
  • You’ll see it is actually using recursion for the iteration, but behaves like a while loop hence the title with the word “while loop” in quotes. But it does follow a while loop-esque format of condition, action, looping until condition is met.

What follows is a sample usage of the promiseWhile() function.

// And below is a sample usage of this promiseWhile functionvar sum = 0,    stop = 10;promiseWhile(function() {    // Condition for stopping    return sum < stop;}, function() {    // Action to run, should return a promise    return new Promise(function(resolve, reject) {        // Arbitrary 250ms async method to simulate async process        // In real usage it could just be a normal async event that         // returns a Promise.        setTimeout(function() {            sum++;            // Print out the sum thus far to show progress            console.log(sum);            resolve();        }, 250);    });}).then(function() {    // Notice we can chain it because it‘s a Promise,     // this will run after completion of the promiseWhile Promise!    console.log("Done");});

And that’s it!

I threw the whole gist on GitHub

Feel free to email me or Tweet me @victorquinn with any questions!

JavaScript: Async Promise “while loop”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.