Цикл while с обещаниями

Что бы быть идиоматические способ сделать что-то вроде цикла while с обещаниями. Так:

что-то делать если состояние все же стоит сделать это снова повторить потом сделать что-то еще.

dosomething.then(possilblydomoresomethings).then(finish)

Я'ве сделали это таким образом, я было интересно, если есть лучше/более idomatic стороны?

var q = require('q');

var index = 1;

var useless =  function(){
        var currentIndex = index;
        console.log(currentIndex)
        var deferred = q.defer();
        setTimeout(function(){
            if(currentIndex > 10)
                deferred.resolve(false);
            else deferred.resolve(true);
            },500);
        return deferred.promise;
    }

var control = function(cont){
        var deferred = q.defer();
        if(cont){
                index = index + 1;
                useless().then(control).then(function(){
                        deferred.resolve();
                    });
            }
         else deferred.resolve();
        return deferred.promise;
    }

var chain = useless().then(control).then(function(){console.log('done')});

Выход: 1 2 3 4 5 6 7 8 9 10 11 сделано

Комментарии к вопросу (4)

Здесь's в функцию для повторного использования, как мне кажется, довольно ясно.


var Q = require("q");

// `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 the result of calling `condition` is no longer true, we are
        // done.
        if (!condition()) return done.resolve();
        // Use `when`, in case `body` does not return a promise.
        // When it completes loop again otherwise, if it fails, reject the
        // done promise
        Q.when(body(), loop, done.reject);
    }

    // Start running the loop in the next tick so that this function is
    // completely async. It would be unexpected if `body` was called
    // synchronously the first time.
    Q.nextTick(loop);

    // The promise
    return done.promise;
}

// Usage
var index = 1;
promiseWhile(function () { return index 
Комментарии (7)

Это самый простой способ я'вэ нашел для выражения основной схеме: вы определяете функцию, которая вызывает обещание, проверяет ее результат, а затем вызывает саму себя снова или прекращается.

в

const doSomething = value =>
  new Promise(resolve => 
    setTimeout(() => resolve(value >= 5 ? 'ok': 'no'), 1000))

const loop = value =>
  doSomething(value).then(result => {
    console.log(value)
    if (result === 'ok') {
      console.log('yay')      
    } else {
      return loop(value + 1)
    }
  })

loop(1).then(() => console.log('all done!'))

[Увидеть его в действии на JSBin](https://jsbin.com/wirumixojo/edit?js консоль)

Если вы используете обещание, которое разрешает или отвергает, вы можете определить тогда и поймать вместо использования если-пункте.

Если вы имели многочисленные обещания, вы бы просто изменить цикл, чтобы сместить или поп следующему каждый раз.


Редактировать: здесь'ы на версию, которая использует асинхронный/ждут, потому что это'ы 2018:

const loop = async value => {
  let result = null
  while (result != 'ok') {
    console.log(value)
    result = await doSomething(value)
    value = value + 1
  }
  console.log('yay')
}

Увидеть его в действии на сайт CodePen

Как вы можете видеть, он использует обычный цикл while и рекурсии.

Комментарии (2)
Решение

Я'd использовать объект, чтобы обернуть ценности. Таким образом, вы можете иметь собственность на "готово", чтобы петли знаю, что вы'повторно сделано.

// fn should return an object like
// {
//   done: false,
//   value: foo
// }
function loop(promise, fn) {
  return promise.then(fn).then(function (wrapper) {
    return !wrapper.done ? loop(Q(wrapper.value), fn) : wrapper.value;
  });
}

loop(Q.resolve(1), function (i) {
  console.log(i);
  return {
    done: i > 10,
    value: i++
  };
}).done(function () {
  console.log('done');
});
Комментарии (4)

Это для Синей птицы не вопрос, но поскольку вы ничего'т упомянуть вопрос конкретно.. в Синей птице по API док автор упоминает возвращение обещание-производящая функция будет более устойчивым, чем через deferreds.

var Promise = require('bluebird');
var i = 0;

var counter = Promise.method(function(){
    return i++;
})

function getAll(max, results){
    var results = results || [];
    return counter().then(function(result){
        results.push(result);
        return (result < max) ? getAll(max, results) : results
    })
}

getAll(10).then(function(data){
    console.log(data);
})
Комментарии (3)

Поскольку я могу'т комментировать Стюарт К'ы ответ Я'будете добавлять немного здесь. На основе Стюарт К'ы ответ можно свести к удивительно простой концепцией: использовать неисполненное обещание. То, что он является по сути:

  1. Создать новый экземпляр отложенное обещание
  2. Определите ваши функции, которые вы хотите позвонить в цикле
  3. Внутри этой функции:
  4. Проверьте, чтобы увидеть, если вы'вновь сделал; и когда вы решить обещание, созданного в #1 и вернуть его.
  5. Если вы это не сделали тогда скажи Q, чтобы использовать существующие обещание и запустить функцию неосуществленные, что в "рекурсивный" и функция, или ошибкой, если он умер. Вопрос: когда(обещают, yourFunction, failFunction)
  6. После определения использование функции Q, чтобы вызвать эту функцию в первый раз, используя В. nextTick(yourFunction)
  7. Наконец-то вернет свой новый обещают абонента (которые будут запускать все это дело для начала).

Стюарт's в ответ на более общие решения, но основы являются удивительными (как только вы поймете, как это работает).

Комментарии (0)

Эта картина теперь более легко вызывается с помощью Q-расход. Например, для вышеуказанной проблемы:

var q = require('q');
require('q-flow');
var index = 1;
q.until(function() {
  return q.delay(500).then(function() {
    console.log(index++);
    return index > 10;
  });
}).done(function() {
  return console.log('done');
});
Комментарии (1)

Вот дополнения к обещание прототип, чтобы имитировать поведение для петли. Он поддерживает обещаний или непосредственных значений для инициализация, условие, тело цикла, и увеличить разделы. Она также имеет полную поддержку исключений, и это не имеет утечек памяти. Ниже приводится один пример о том, как использовать его.

var Promise = require('promise');

// Promise.loop([properties: object]): Promise()
//
//  Execute a loop based on promises. Object 'properties' is an optional
//  argument with the following fields:
//
//  initialization: function(): Promise() | any, optional
//
//      Function executed as part of the initialization of the loop. If
//      it returns a promise, the loop will not begin to execute until
//      it is resolved.
//
//      Any exception occurring in this function will finish the loop
//      with a rejected promise. Similarly, if this function returns a
//      promise, and this promise is reject, the loop finishes right
//      away with a rejected promise.
//
//  condition: function(): Promise(result: bool) | bool, optional
//
//      Condition evaluated in the beginning of each iteration of the
//      loop. The function should return a boolean value, or a promise
//      object that resolves with a boolean data value.
//
//      Any exception occurring during the evaluation of the condition
//      will finish the loop with a rejected promise. Similarly, it this
//      function returns a promise, and this promise is rejected, the
//      loop finishes right away with a rejected promise.
//
//      If no condition function is provided, an infinite loop is
//      executed.
//
//  body: function(): Promise() | any, optional
//
//      Function acting as the body of the loop. If it returns a
//      promise, the loop will not proceed until this promise is
//      resolved.
//
//      Any exception occurring in this function will finish the loop
//      with a rejected promise. Similarly, if this function returns a
//      promise, and this promise is reject, the loop finishes right
//      away with a rejected promise.
//
//  increment: function(): Promise() | any, optional
//
//      Function executed at the end of each iteration of the loop. If
//      it returns a promise, the condition of the loop will not be
//      evaluated again until this promise is resolved.
//
//      Any exception occurring in this function will finish the loop
//      with a rejected promise. Similarly, if this function returns a
//      promise, and this promise is reject, the loop finishes right
//      away with a rejected promise.
//
Promise.loop = function(properties)
{
    // Default values
    properties = properties || {};
    properties.initialization = properties.initialization || function() { };
    properties.condition = properties.condition || function() { return true; };
    properties.body = properties.body || function() { };
    properties.increment = properties.increment || function() { };

    // Start
    return new Promise(function(resolve, reject)
    {
        var runInitialization = function()
        {
            Promise.resolve().then(function()
            {
                return properties.initialization();
            })
            .then(function()
            {
                process.nextTick(runCondition);
            })
            .catch(function(error)
            {
                reject(error);
            });
        }

        var runCondition = function()
        {
            Promise.resolve().then(function()
            {
                return properties.condition();
            })
            .then(function(result)
            {
                if (result)
                    process.nextTick(runBody);
                else
                    resolve();
            })
            .catch(function(error)
            {
                reject(error);
            });
        }

        var runBody = function()
        {
            Promise.resolve().then(function()
            {
                return properties.body();
            })
            .then(function()
            {
                process.nextTick(runIncrement);
            })
            .catch(function(error)
            {
                reject(error);
            });
        }

        var runIncrement = function()
        {
            Promise.resolve().then(function()
            {
                return properties.increment();
            })
            .then(function()
            {
                process.nextTick(runCondition);
            })
            .catch(function(error)
            {
                reject(error);
            });
        }

        // Start running initialization
        process.nextTick(runInitialization);
    });
}

// Promise.delay(time: double): Promise()
//
//  Returns a promise that resolves after the given delay in seconds.
//
Promise.delay = function(time)
{
    return new Promise(function(resolve)
    {
        setTimeout(resolve, time * 1000);
    });
}

// Example
var i;
Promise.loop({
    initialization: function()
    {
        i = 2;
    },
    condition: function()
    {
        return i < 6;
    },
    body: function()
    {
        // Print "i"
        console.log(i);

        // Exception when 5 is reached
        if (i == 5)
            throw Error('Value of "i" reached 5');

        // Wait 1 second
        return Promise.delay(1);
    },
    increment: function()
    {
        i++;
    }
})
.then(function()
{
    console.log('LOOP FINISHED');
})
.catch(function(error)
{
    console.log('EXPECTED ERROR:', error.message);
});
Комментарии (0)

Я сейчас использую этот:

function each(arr, work) {
  function loop(arr, i) {
    return new Promise(function(resolve, reject) {
      if (i >= arr.length) {resolve();}
      else try {
        Promise.resolve(work(arr[i], i)).then(function() { 
          resolve(loop(arr, i+1))
        }).catch(reject);
      } catch(e) {reject(e);}
    });
  }
  return loop(arr, 0);
}

Это принимает массив Арр и функция и возвращает обещание. Предоставленная функция вызывается один раз для каждого элемента в массиве и передается текущий элемент и он'индекс S в массиве. Это может быть синхронный или асинхронный, и в этом случае он должен возвращать обещание.

Вы можете использовать его как это:

var items = ['Hello', 'cool', 'world'];
each(items, function(item, idx) {
    // this could simply be sync, but can also be async
    // in which case it must return a Promise
    return new Promise(function(resolve){
        // use setTimeout to make this async
        setTimeout(function(){
            console.info(item, idx);
            resolve();
        }, 1000);
    });
})
.then(function(){
    console.info('DONE');
})
.catch(function(error){
    console.error('Failed', error);
})

Каждый элемент в массиве будут обрабатываться по очереди. После того, как все обрабатываются, код дали .тогда () будет работать, или, если произошла какая-то ошибка, код дали.поймать(). Внутри "работу" функции, вы можете "бросок"ошибки` (в случае синхронной функции) или "отклонить" на "обещания" (в случае асинхронных функций), чтобы прервать цикл.

в

function each(arr, work) {
  function loop(arr, i) {
    return new Promise(function(resolve, reject) {
      if (i >= arr.length) {resolve();}
      else try {
        Promise.resolve(work(arr[i], i)).then(function() { 
          resolve(loop(arr, i+1))
        }).catch(reject);
      } catch(e) {reject(e);}
    });
  }
  return loop(arr, 0);
}

var items = ['Hello', 'cool', 'world'];
each(items, function(item, idx) {
  // this could simply be sync, but can also be async
  // in which case it must return a Promise
  return new Promise(function(resolve){
    // use setTimeout to make this async
    setTimeout(function(){
      console.info(item, idx);
      resolve();
    }, 1000);
  });
})
.then(function(){
  console.info('DONE');
})
.catch(function(error){
  console.error('Failed', error);
})

в

Комментарии (0)
var Q = require('q')

var vetor  = ['a','b','c']

function imprimeValor(elements,initValue,defer){

    console.log( elements[initValue++] )
    defer.resolve(initValue)
    return defer.promise
}

function Qloop(initValue, elements,defer){

    Q.when( imprimeValor(elements, initValue, Q.defer()), function(initValue){

        if(initValue===elements.length){
            defer.resolve()
        }else{
            defer.resolve( Qloop(initValue,elements, Q.defer()) )
        }
    }, function(err){

        defer.reject(err)
    })

    return defer.promise
}

Qloop(0, vetor,Q.defer())
Комментарии (1)

Много ответов здесь и то, что вы пытаетесь достичь, это не очень практично. но это должно работать. Это было реализовано в функцию AWS лямбда, с Node.js 10 он будет идти до тайм-аута функция. Он также может потреблять приличное количество памяти.

exports.handler = async (event) => {
  let res = null;
  while (true) {
    try{
     res = await dopromise();
    }catch(err){
     res = err;
    }
    console.log(res);
   }//infinite will time out
  };

  function dopromise(){
   return new Promise((resolve, reject) => {
    //do some logic
    //if error reject
        //reject('failed');
    resolve('success');
  });
}

Проверено на лямда и работает нормально на протяжении 5 минут. Но как говорится, это не хорошая вещь, чтобы сделать.

Комментарии (0)

Используя ЕС6 обещаю, я придумал это. Это цепи обещаний и возвращает обещание. Это's не технически цикла while, но как перебрать обещает синхронно.

в

function chain_promises(list, fun) {
    return list.reduce(
        function (promise, element) {
            return promise.then(function () {
                // I only needed to kick off some side-effects. If you need to get
                // a list back, you would append to it here. Or maybe use
                // Array.map instead of Array.reduce.
                fun(element);
            });
        },
        // An initial promise just starts things off.
        Promise.resolve(true)
    );
}

// To test it...

function test_function (element) {
    return new Promise(function (pass, _fail) {
        console.log('Processing ' + element);
        pass(true);
    });
}

chain_promises([1, 2, 3, 4, 5], test_function).then(function () {
    console.log('Done.');
});

в

[Здесь'с моей скрипкой.][1]

Комментарии (2)

Я думал, я мог бы также бросить свою шляпу в кольцо, используя на ES6 обещания...

function until_success(executor){
    var before_retry = undefined;
    var outer_executor = function(succeed, reject){
        var rejection_handler = function(err){
            if(before_retry){
                try {
                    var pre_retry_result = before_retry(err);
                    if(pre_retry_result)
                        return succeed(pre_retry_result);
                } catch (pre_retry_error){
                    return reject(pre_retry_error);
                }
            }
            return new Promise(executor).then(succeed, rejection_handler);                
        }
        return new Promise(executor).then(succeed, rejection_handler);
    }

    var outer_promise = new Promise(outer_executor);
    outer_promise.before_retry = function(func){
        before_retry = func;
        return outer_promise;
    }
    return outer_promise;
}

В исполнитель аргумент то же как то прошел на обещание конструктор, но будет называться до тех пор, пока это вызывает успех обратного вызова. Функция before_retry позволяет пользовательские обработки ошибок на неудачные попытки. Если она возвращает значение ИСТИНА, он будет рассматриваться как одна из форм успеха и "петли" и закончится, что истина как результат. Если нет before_retry функциязарегистрирована, или она возвращает значение falsey, то цикл будет выполняться повторно. Третий вариант заключается в том, что функцияbefore_retry` бросает себя ошибку. Если это произойдет, то в "петли" и закончится, передавая эту ошибку как ошибку.


Вот пример:

var counter = 0;
function task(succ, reject){
    setTimeout(function(){
        if(++counter < 5)
            reject(counter + " is too small!!");
        else
            succ(counter + " is just right");
    }, 500); // simulated async task
}

until_success(task)
        .before_retry(function(err){
            console.log("failed attempt: " + err);
            // Option 0: return falsey value and move on to next attempt
            // return

            // Option 1: uncomment to get early success..
            //if(err === "3 is too small!!") 
            //    return "3 is sort of ok"; 

            // Option 2: uncomment to get complete failure..
            //if(err === "3 is too small!!") 
            //    throw "3rd time, very unlucky"; 
  }).then(function(val){
       console.log("finally, success: " + val);
  }).catch(function(err){
       console.log("it didn't end well: " + err);
  })

Выходной параметр 0:

failed attempt: 1 is too small!!
failed attempt: 2 is too small!!
failed attempt: 3 is too small!!
failed attempt: 4 is too small!!
finally, success: 5 is just right

Выход для варианта 1:

failed attempt: 1 is too small!!
failed attempt: 2 is too small!!
failed attempt: 3 is too small!!
finally, success: 3 is sort of ok

Выход для варианта 2:

failed attempt: 1 is too small!!
failed attempt: 2 is too small!!
failed attempt: 3 is too small!!
it didn't end well: 3rd time, very unlucky
Комментарии (0)

Я написал модуль, который поможет вам сделать скованные петли асинхронных задач с обещаниями, он основан на выше ответ juandopazo

/**
 * Should loop over a task function which returns a "wrapper" object
 * until wrapper.done is true. A seed value wrapper.seed is propagated to the
 * next run of the loop.
 *
 * todo/maybe? Reject if wrapper is not an object with done and seed keys.
 *
 * @param {Promise|*} seed
 * @param {Function} taskFn
 *
 * @returns {Promise.}
 */
function seedLoop(seed, taskFn) {
  const seedPromise = Promise.resolve(seed);

  return seedPromise
    .then(taskFn)
    .then((wrapper) => {
      if (wrapper.done) {
        return wrapper.seed;
      }

      return seedLoop(wrapper.seed, taskFn);
    });
}

// A super simple example of counting to ten, which doesn't even
// do anything asynchronous, but if it did, it should resolve to 
// a promise that returns the { done, seed } wrapper object for the
// next call of the countToTen task function.
function countToTen(count) {
  const done = count > 10;
  const seed = done ? count : count + 1;

  return {done, seed};
}

seedLoop(1, countToTen).then((result) => {
  console.log(result); // 11, the first value which was over 10.
});

https://github.com/CascadeEnergy/promise-seedloop

Комментарии (0)