Javascript 建议练习:确保只有在发生的特定情况得到解决后才需要履行承诺
我有一个承诺,其抽象代码如下:Javascript 建议练习:确保只有在发生的特定情况得到解决后才需要履行承诺,javascript,promise,Javascript,Promise,我有一个承诺,其抽象代码如下: const myPromise = (input) => new Promise((resolve, reject) => { //does something with input and provide some result if (everything_is_ok) resolve(result); else reject(error); }); if (condition){ myPromise(takes myV
const myPromise = (input) => new Promise((resolve, reject) => {
//does something with input and provide some result
if (everything_is_ok) resolve(result);
else reject(error);
});
if (condition){
myPromise(takes myVar or some data as input here).then((pr_output)=>{myVar=compute(pr_output);
anotherProcedure(myVar); // <== THIS IS IT
});
} else anotherPocedure(myVar) // <== AND... THIS IS IT TOO
这是我脚本中一个过程的抽象流程:
let myVar;
//some code instructions...
myVar = something; //this comes as result of some code
if (condition){
//(once promises resolves) compute function does something with pr_output
//and provides another resulting output that gets stored on myVar for further computation
myPromise(takes myVar or some data as input here).then((pr_output)=>{myVar=compute(pr_output);});
}
//further operations with myVar follow here...
//AND, if condition was true, I want/need to be sure that the promise has resolved
//and the computation in its "then" instruction ended as well before going on...
所以现在的问题是:
(如何)不必调用后续函数就可以继续进行操作?
我的意思是我知道我可以做一些简单的事情,比如:
const myPromise = (input) => new Promise((resolve, reject) => {
//does something with input and provide some result
if (everything_is_ok) resolve(result);
else reject(error);
});
if (condition){
myPromise(takes myVar or some data as input here).then((pr_output)=>{myVar=compute(pr_output);
anotherProcedure(myVar); // <== THIS IS IT
});
} else anotherPocedure(myVar) // <== AND... THIS IS IT TOO
if(条件){
myPromise(在这里接受myVar或一些数据作为输入),然后((pr_输出)=>{myVar=compute(pr_输出);
另一个过程(myVar);//只创建一个Promise
链,您可以将anotherProcedure(myVar)
附加到它的末尾。如果条件为true,则返回myPromise
调用(从而“暂停”Promise
链,直到它解决为止),否则不返回任何内容(因此运行下一个。然后立即运行另一个过程的)。翻译较低的代码时,它可能看起来像
Promise.resolve()
.then(() => {
if (condition) return myPromise(takes myVar or some data as input here)
.then((pr_output) => {
myVar = compute(pr_output);
});
})
.then(() => anotherPocedure(myVar));
虽然将第一个。然后
提取到它自己的函数中会更具可读性,但为了更好的可读性:
const tryCompute = () => {
if (condition) return myPromise(takes myVar or some data as input here)
.then((pr_output) => {
myVar = compute(pr_output);
});
else return Promise.resolve();
};
tryCompute()
.then(() => anotherPocedure(myVar));
只创建一个Promise
链,您可以将另一个Promise(myVar)
附加到它的末尾。如果条件为true,则返回myPromise
调用(从而“暂停”Promise
链,直到它解决为止),否则不返回任何内容(因此运行下一个。然后立即运行另一个过程的)。翻译较低的代码时,它可能看起来像
Promise.resolve()
.then(() => {
if (condition) return myPromise(takes myVar or some data as input here)
.then((pr_output) => {
myVar = compute(pr_output);
});
})
.then(() => anotherPocedure(myVar));
虽然将第一个。然后
提取到它自己的函数中会更具可读性,但为了更好的可读性:
const tryCompute = () => {
if (condition) return myPromise(takes myVar or some data as input here)
.then((pr_output) => {
myVar = compute(pr_output);
});
else return Promise.resolve();
};
tryCompute()
.then(() => anotherPocedure(myVar));
正如注释中所建议的,您可以简单地使用async/await:
(async function() {
if(condition) {
const myVar = compute( await myPromise());
}
anotherProcedure();
})();
正如注释中所建议的,您可以简单地使用async/await:
(async function() {
if(condition) {
const myVar = compute( await myPromise());
}
anotherProcedure();
})();
您可能想看看,如果myPromise
进行验证,为什么它需要异步?如果您只是想避免命名另一个过程
函数,请看一看。如果您想避免编写(匿名)总之,您唯一的选择是async
/wait
。是的,@tevemadar您是对的(我一开始尝试过,但有一个bug,在意识到真正的错误之前,我认为我在使用async/await
时犯了一些错误+1@Bergi你真的必须是承诺
福音传道者:-D非常感谢你在评论中链接这个问题,我觉得这里+1,那里+1…非常有趣再次感谢!:-)您可能想看一看,如果myPromise
进行验证,为什么它需要异步?如果您只是想避免命名另一个过程
函数,请看一看。如果您想避免编写(匿名)总之,您唯一的选择是async
/wait
。是的,@tevemadar您是对的(我一开始尝试过,但有一个bug,在意识到真正的错误之前,我认为我在使用async/await
时犯了一些错误+1@Bergi你真的必须是承诺
福音传道者:-D非常感谢你在评论中链接这个问题,我觉得这里+1,那里+1…非常有趣再次感谢!:-)我喜欢这样做,非常感谢,并为我的延迟感到抱歉。我在询问之前尝试过类似的方法,但似乎不起作用,我认为我在使用async/wait
…在您的回复后,我发现了一个(非常愚蠢但很难看到)计算代码facepalm中的错误:-p…再次感谢,接受。我喜欢这样做,非常感谢,并对我的延迟表示抱歉。在询问之前,我尝试过类似的方法,但似乎不起作用,我认为我在async/wait
方面做错了什么。在您的回复之后,我发现了一个错误(非常愚蠢,但很难看到)计算代码中的错误facepalm:-p…再次感谢您,接受。感谢您的回复,如果可以的话,我也会接受这个,但我只能选择一个,并且非常喜欢Jonas W。示例…(请向上投票,ofc。)谢谢你的回复,如果可以的话,我也会接受这个,但我只能选择一个,并且非常喜欢Jonas W.example…(请向上投票,ofc.)