Node.js 而承诺则是一个循环
什么是惯用的方式来做一些事情,比如带着承诺的while循环。因此: 做点什么 如果情况仍然存在,请再次执行 重复 然后做点别的Node.js 而承诺则是一个循环,node.js,loops,promise,q,Node.js,Loops,Promise,Q,什么是惯用的方式来做一些事情,比如带着承诺的while循环。因此: 做点什么 如果情况仍然存在,请再次执行 重复 然后做点别的 dosomething.then(possilblydomoresomethings).then(finish) 我是这样做的,我想知道是否有更好/更具自动化的方法 var q = require('q'); var index = 1; var useless = function(){ var currentIndex = index;
dosomething.then(possilblydomoresomethings).then(finish)
我是这样做的,我想知道是否有更好/更具自动化的方法
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
完成我认为这是一个非常清晰的可重用函数
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 <= 11; }, function () {
console.log(index);
index++;
return Q.delay(500); // arbitrary async
}).then(function () {
console.log("done");
}).done();
var Q=require(“Q”);
//'condition'是一个返回布尔值的函数
//'body'是一个返回承诺的函数
//返回完成循环的承诺
功能承诺(条件、主体){
var done=Q.defer();
函数循环(){
//当调用'condition'的结果不再为真时,我们就是
//完成了。
如果(!condition())返回done.resolve();
//如果“body”不返回承诺,请使用“when”。
//当它再次完成循环时,否则,如果失败,则拒绝
//答应
Q.when(body(),循环,done.reject);
}
//在下一个勾号中开始运行循环,以便此函数
//完全异步。如果调用'body',这将是意外的
//第一次同步。
Q.nextTick(循环);
//承诺
回报完成。承诺;
}
//用法
var指数=1;
promisewile(function(){returnindex我会使用一个对象来包装这个值。这样你就可以有一个done
属性来让循环知道你已经完成了
// 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');
});
现在使用更容易调用此模式。例如,对于上述问题:
var q=require('q');
要求(“q流”);
var指数=1;
q、 直到(函数(){
返回q.delay(500)。然后(函数(){
log(index++);
收益指数>10;
});
}).done(函数(){
返回console.log('done');
});
这是针对bluebird的,不是q,而是因为您没有特别提到q。在bluebird api文档中,作者提到返回承诺生成函数比使用延迟函数更惯用
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);
})
var Promise=require('bluebird');
var i=0;
var counter=Promise.method(函数(){
返回i++;
})
函数getAll(最大值,结果){
var结果=结果| |[];
返回计数器()。然后(函数(结果){
结果。推(结果);
返回(结果<最大值)?getAll(最大值,结果):结果
})
}
getAll(10)。然后(函数(数据){
控制台日志(数据);
})
这里是对Promise
原型的扩展,用于模拟for
循环的行为。它支持初始化、条件、循环体和增量部分的承诺或立即值。它还完全支持异常,并且没有内存泄漏。下面给出了一个如何使用它的示例
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);
});
var Promise=require('Promise');
//loop([properties:object]):Promise()
//
//基于承诺执行循环。对象“属性”是可选的
//具有以下字段的参数:
//
//初始化:函数():承诺()|任意,可选
//
//作为循环初始化的一部分执行的函数。如果
//它返回一个承诺,直到
//问题解决了。
//
//此函数中发生的任何异常都将完成循环
//类似地,如果此函数返回
//承诺,这个承诺被拒绝,循环正确完成
//带着被拒绝的诺言离开。
//
//条件:函数():承诺(结果:bool)| bool,可选
//
//在每个迭代开始时评估的条件
//函数应该返回一个布尔值或一个承诺
//对象,该对象使用布尔数据值进行解析。
//
//条件评估期间发生的任何异常
//将以拒绝的承诺完成循环。类似地,它
//函数返回一个承诺,如果该承诺被拒绝
//循环立即结束,承诺被拒绝。
//
//如果未提供任何条件函数,则将生成一个无限循环
//执行。
//
//正文:函数():承诺()|任意,可选
//
//函数作为循环体。如果它返回
//承诺,直到这个承诺兑现,循环才会继续
//决心。
//
//此函数中发生的任何异常都将完成循环
//类似地,如果此函数返回
//承诺,这个承诺被拒绝,循环正确完成
//带着被拒绝的诺言离开。
//
//增量:函数():承诺()|任意,可选
//
//在循环每次迭代结束时执行的函数。如果
//它返回一个promise,循环的条件将不会改变
//再次评估,直到该承诺得到解决。
//
//此函数中发生的任何异常都将完成循环
//类似地,如果此函数返回
//承诺,这个承诺被拒绝,循环正确完成
//带着被拒绝的诺言离开。
//
Promise.loop=函数(属性)
{
//默认值
属性=属性| |{};
properties.initialization=properties.initialization | | function(){};
properties.condition=properties.condition | | function(){return true;};
properties.body=properties.body | | function(){};
properties.increment=properties.increment | | function(){};
//开始
返回新承诺(功能(解决、拒绝)
{
var runInitialization=函数()
{
Promise.resolve().then(函数())
{
返回properties.initialization();
})
.然后(函数()
{
进程.nextTick(运行条件);
})
.catch(函数(错误)
{
拒绝(错误);
});
}
var runCondition=函数()
{
Promise.resolve().then(函数())
{
回程道具
/**
* 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.
});
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())
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;
}
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);
})
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
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
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
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);
})
const loop = async value => {
let result = null
while (result != 'ok') {
console.log(value)
result = await doSomething(value)
value = value + 1
}
console.log('yay')
}
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');
});
}