Javascript 承诺如何进行 post 分辨率清理
Javascript Promises how to do post resolution cleanup
我正在为如何处理这种特殊情况而苦恼。我知道我可以通过链式回调来解决这个问题,但它似乎几乎是 promises 的典型代表:
我有一个父方法需要按顺序执行三个异步操作(特别是从用户那里获得确认)。我们称它们为 func1 func2 和 func3。现在我可以让这些 return 中的每一个都有一个 promise 并将它们链接起来,这一切都很好。我 运行 遇到的问题是:
func1 需要设置状态,等待链的其余部分到 运行,然后取消设置该状态。
演示伪代码:
function wrapper(){
func1()
.then(func2())
.then(func3());
}
function func1(){
return new Promise(function(resolve, reject){
//do some async stuff that itself returns a promise :)
async1().then(function(value){
//set some global states based on the value for the duration of this chain
resolve(); //Note NOT returning value as it's irrelevant to the other functions
//!!! clean up the global states regardless of future failure or success, but after one of those.
}, reject); //if async1 rejects, just pass that right up.
});
}
function func2(){
//do some logic here to decide which asyn function to call
if (/*evaluates to */true){
return async2(); //another async function that returns a promise, yay!
} else {
return async2_1(); //also returns a promise.
}
}
function func3(){
//do some dom updates to let the user know what's going on
return Promise.resolve(async3()).then(function(){
//update the dom to let them know that everything went well.
});//returns this promise chain as a promise which the other chain will use.
}
我正在努力解决的部分是 func1 中 resolve() 之后的那一行;请注意,如前所述,我在 func1 中调用的 async1 做了 return 承诺,所以我已经在这里处理了很多承诺。我需要在 func3 的承诺 returned 解决后进行清理。
理想情况下,这将以某种方式全部包含在 func1 中,但我也可以使用半全局变量(整个块将包含在一个更大的函数中)。
您需要使用 (and avoid the Promise
constructor antipattern):
function with1(callback) {
return async1() // do some async stuff that itself returns a promise :)
.then(function(value) {
// set some global states based on the value for the duration of this chain
const result = Promise.resolve(value) // or whatever
.then(callback);
return result.then(cleanup, cleanup);
function cleanup() {
// clean up the global states regardless of the failure or success of result
return result;
}
});
}
你可以这样使用
function wrapper() {
with1(() =>
func2().then(func3) // whatever should run in the altered context
);
}
我正在为如何处理这种特殊情况而苦恼。我知道我可以通过链式回调来解决这个问题,但它似乎几乎是 promises 的典型代表:
我有一个父方法需要按顺序执行三个异步操作(特别是从用户那里获得确认)。我们称它们为 func1 func2 和 func3。现在我可以让这些 return 中的每一个都有一个 promise 并将它们链接起来,这一切都很好。我 运行 遇到的问题是:
func1 需要设置状态,等待链的其余部分到 运行,然后取消设置该状态。
演示伪代码:
function wrapper(){
func1()
.then(func2())
.then(func3());
}
function func1(){
return new Promise(function(resolve, reject){
//do some async stuff that itself returns a promise :)
async1().then(function(value){
//set some global states based on the value for the duration of this chain
resolve(); //Note NOT returning value as it's irrelevant to the other functions
//!!! clean up the global states regardless of future failure or success, but after one of those.
}, reject); //if async1 rejects, just pass that right up.
});
}
function func2(){
//do some logic here to decide which asyn function to call
if (/*evaluates to */true){
return async2(); //another async function that returns a promise, yay!
} else {
return async2_1(); //also returns a promise.
}
}
function func3(){
//do some dom updates to let the user know what's going on
return Promise.resolve(async3()).then(function(){
//update the dom to let them know that everything went well.
});//returns this promise chain as a promise which the other chain will use.
}
我正在努力解决的部分是 func1 中 resolve() 之后的那一行;请注意,如前所述,我在 func1 中调用的 async1 做了 return 承诺,所以我已经在这里处理了很多承诺。我需要在 func3 的承诺 returned 解决后进行清理。
理想情况下,这将以某种方式全部包含在 func1 中,但我也可以使用半全局变量(整个块将包含在一个更大的函数中)。
您需要使用 Promise
constructor antipattern):
function with1(callback) {
return async1() // do some async stuff that itself returns a promise :)
.then(function(value) {
// set some global states based on the value for the duration of this chain
const result = Promise.resolve(value) // or whatever
.then(callback);
return result.then(cleanup, cleanup);
function cleanup() {
// clean up the global states regardless of the failure or success of result
return result;
}
});
}
你可以这样使用
function wrapper() {
with1(() =>
func2().then(func3) // whatever should run in the altered context
);
}