我想做什么
getFoo()
.then(doA)
.then(doB)
.then(function(){
ifC().then(function(res){
if(res) return doC();
else return doElse();
});
我认为代码非常明显?无论如何:
我希望在给出特定条件(也是承诺)时调用承诺。我可能会做类似
的事情var sass = require('../sass/authenty.scss');
但这感觉非常冗长。
我正在使用bluebird作为promise库。但我想如果有类似的东西,它会在任何诺言库中都一样。
答案 0 :(得分:4)
您不需要嵌套.then
次来电,因为ifC
似乎还会返回Promise
:
getFoo()
.then(doA)
.then(doB)
.then(ifC)
.then(function(res) {
if (res) return doC();
else return doElse();
});
你也可以预先做一些腿部工作:
function myIf( condition, ifFn, elseFn ) {
return function() {
if ( condition.apply(null, arguments) )
return ifFn();
else
return elseFn();
}
}
getFoo()
.then(doA)
.then(doB)
.then(ifC)
.then(myIf(function(res) {
return !!res;
}, doC, doElse ));
答案 1 :(得分:2)
我认为您正在寻找类似this
的内容您的代码示例:
getFoo()
.then(doA)
.then(doB)
.then(condition ? doC() : doElse());
必须在开始链之前定义条件中的元素。
答案 2 :(得分:2)
基于Using the Dialog APIs in an authentication flow,这是我想出的可选项:
注意:如果您的条件函数确实需要作出承诺,请查看@ TbWill4321的答案
回答可选 <div id="one" class="click" data-section="section_1"></div>
<div id="two" class="click" data-section="section_2"></div>
<div id="three" class="click" data-section="section_3"></div>
<div id="four" class="click" data-section="section_4"></div>
<div class="sections" id="section_1">
<div class="box"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi lobortis augue quis lectus sollicitudin venenatis. Vestibulum quam nisi, condimentum ac diam ut, aliquam dignissim odio. Nulla nulla arcu, convallis quis ipsum tempor, volutpat placerat
magna. Praesent non neque maximus, sollicitudin eros vitae, mattis augue. Donec ac semper purus. Vivamus a elit eu risus porttitor mollis. Cras efficitur quam sed nisi facilisis volutpat. Mauris in arcu nec magna ullamcorper luctus. Cras non placerat
neque.
</p>
</div>
<div class="sections" id="section_2">
<div class="box"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi lobortis augue quis lectus sollicitudin venenatis. Vestibulum quam nisi, condimentum ac diam ut, aliquam dignissim odio. Nulla nulla arcu, convallis quis ipsum tempor, volutpat placerat
magna. Praesent non neque maximus, sollicitudin eros vitae, mattis augue. Donec ac semper purus. Vivamus a elit eu risus porttitor mollis. Cras efficitur quam sed nisi facilisis volutpat. Mauris in arcu nec magna ullamcorper luctus. Cras non placerat
neque.
</p>
</div>
<div class="sections" id="section_3">
<div class="box"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi lobortis augue quis lectus sollicitudin venenatis. Vestibulum quam nisi, condimentum ac diam ut, aliquam dignissim odio. Nulla nulla arcu, convallis quis ipsum tempor, volutpat placerat
magna. Praesent non neque maximus, sollicitudin eros vitae, mattis augue. Donec ac semper purus. Vivamus a elit eu risus porttitor mollis. Cras efficitur quam sed nisi facilisis volutpat. Mauris in arcu nec magna ullamcorper luctus. Cras non placerat
neque.
</p>
</div>
<div class="sections" id="section_4">
<div class="box"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi lobortis augue quis lectus sollicitudin venenatis. Vestibulum quam nisi, condimentum ac diam ut, aliquam dignissim odio. Nulla nulla arcu, convallis quis ipsum tempor, volutpat placerat
magna. Praesent non neque maximus, sollicitudin eros vitae, mattis augue. Donec ac semper purus. Vivamus a elit eu risus porttitor mollis. Cras efficitur quam sed nisi facilisis volutpat. Mauris in arcu nec magna ullamcorper luctus. Cras non placerat
neque.
</p>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
then()
来自@jacksmirk的条件 getFoo()
.then(doA)
.then(doB)
.then((b) => { ifC(b) ? doC(b) : Promise.resolve(b) }) // to be able to skip doC()
.then(doElse) // doElse will run if all the previous resolves
then()
编辑:我建议你看一下蓝鸟关于
的讨论getFoo() .then(doA) .then(doB) .then((b) => { ifC(b) ? doC(b) : doElse(b) }); // will execute either doC() or doElse()
this other question