Promise 链中类似递归的行为
Recursion-like behaviour in a Promise chain
我目前正在努力实现对特定 Promiseblock 的递归。
我将我的代码抽象成这样以提供示例:
function x(){
return new Promise((resolve, reject)=>{
return resolve(2)
})
}
var testvar = 0
x()
.then(result => {
// simulating mongodb request which returns another promise
return new Promise(resolve => {resolve()})
})
.then(result => { // BLOCK START
// simulating another operation
testvar += 1
return new Promise(resolve => {resolve()})
})
.then(result => {
// some other operations
if(testvar < 2){
// RERUN PROMISE FROM BLOCK START
console.log("pls recurse")
}else{
// some other operation
return new Promise(resolve => {resolve()})
}
// BLOCK END
})
.then(result => {
// continue
console.log("foo")
})
.catch(err => console.log(err))
// classic approach
function y(){
// something
// Operation 1
// Operation 2
if(x != 1 ){
y() // recurse
}else{
// continue
}
}
现在我想要这段代码做的是 运行 Promisechain 一个接一个直到最后一个(记录 "foo" 的那个)。除非 testvar 小于 2,否则我希望再次执行“// BLOCK START”中的函数,直到 testvar 大于或等于 2。
我依赖于这个基于 Promise 的构建,因为我正在对辅助库和 returns 承诺的 mongodb 进行一些异步函数调用。
代码也可以在fiddle
中测试
如果有任何不清楚的地方,请随时提问 - 我很乐意尝试准确地提出我的问题。
感谢您的帮助。
与普通的递归函数没有太大区别。您可以将您的代码移动到 runBlock
函数中,并且在您的 if
条件下您可以再次调用 return runBlock(result);
或您的 return 您已解决的 Promise:
function x() {
return new Promise((resolve, reject) => {
return resolve(2)
})
}
var testvar = 0
function runBlock(result) {
testvar += 1
return new Promise(resolve => {
resolve()
})
.then(result => {
// some other operations
if (testvar < 2) {
console.log('testvar < 2');
// RERUN PROMISE FROM BLOCK START
return runBlock(result);
} else {
console.log('testvar >= 2');
// some other operation
return new Promise(resolve => {
resolve()
})
}
// BLOCK END
})
}
x()
.then(result => {
// simulating mongodb request which returns another promise
return new Promise(resolve => {
resolve()
})
})
.then(runBlock)
.then(result => {
// continue
console.log("foo")
})
.catch(err => console.log(err))
function a(a) {
return new Promise(resolve => {
resolve(a)
})
}
// classic approach
function y() {
// something
// Operation 1
// Operation 2
if (x != 1) {
y() // recurse
} else {
// continue
}
}
这可能是@t.niese 承诺中的递归的简化版本。可以这样操作;
var pr = Promise.resolve(1),
fulFillment = v => Promise.resolve(v*=2)
.then(v => v > 100 ? "foo" : (console.log(v), fulFillment(v)));
pr.then(fulFillment)
.then(v => console.log(v))
.catch(e => console.log(e));
我目前正在努力实现对特定 Promiseblock 的递归。
我将我的代码抽象成这样以提供示例:
function x(){
return new Promise((resolve, reject)=>{
return resolve(2)
})
}
var testvar = 0
x()
.then(result => {
// simulating mongodb request which returns another promise
return new Promise(resolve => {resolve()})
})
.then(result => { // BLOCK START
// simulating another operation
testvar += 1
return new Promise(resolve => {resolve()})
})
.then(result => {
// some other operations
if(testvar < 2){
// RERUN PROMISE FROM BLOCK START
console.log("pls recurse")
}else{
// some other operation
return new Promise(resolve => {resolve()})
}
// BLOCK END
})
.then(result => {
// continue
console.log("foo")
})
.catch(err => console.log(err))
// classic approach
function y(){
// something
// Operation 1
// Operation 2
if(x != 1 ){
y() // recurse
}else{
// continue
}
}
现在我想要这段代码做的是 运行 Promisechain 一个接一个直到最后一个(记录 "foo" 的那个)。除非 testvar 小于 2,否则我希望再次执行“// BLOCK START”中的函数,直到 testvar 大于或等于 2。
我依赖于这个基于 Promise 的构建,因为我正在对辅助库和 returns 承诺的 mongodb 进行一些异步函数调用。
代码也可以在fiddle
中测试如果有任何不清楚的地方,请随时提问 - 我很乐意尝试准确地提出我的问题。 感谢您的帮助。
与普通的递归函数没有太大区别。您可以将您的代码移动到 runBlock
函数中,并且在您的 if
条件下您可以再次调用 return runBlock(result);
或您的 return 您已解决的 Promise:
function x() {
return new Promise((resolve, reject) => {
return resolve(2)
})
}
var testvar = 0
function runBlock(result) {
testvar += 1
return new Promise(resolve => {
resolve()
})
.then(result => {
// some other operations
if (testvar < 2) {
console.log('testvar < 2');
// RERUN PROMISE FROM BLOCK START
return runBlock(result);
} else {
console.log('testvar >= 2');
// some other operation
return new Promise(resolve => {
resolve()
})
}
// BLOCK END
})
}
x()
.then(result => {
// simulating mongodb request which returns another promise
return new Promise(resolve => {
resolve()
})
})
.then(runBlock)
.then(result => {
// continue
console.log("foo")
})
.catch(err => console.log(err))
function a(a) {
return new Promise(resolve => {
resolve(a)
})
}
// classic approach
function y() {
// something
// Operation 1
// Operation 2
if (x != 1) {
y() // recurse
} else {
// continue
}
}
这可能是@t.niese 承诺中的递归的简化版本。可以这样操作;
var pr = Promise.resolve(1),
fulFillment = v => Promise.resolve(v*=2)
.then(v => v > 100 ? "foo" : (console.log(v), fulFillment(v)));
pr.then(fulFillment)
.then(v => console.log(v))
.catch(e => console.log(e));