简体   繁体   English

Ember中的条件承诺

[英]Conditional promises in Ember.RSVP

I have a simple if else scenario where I need to do something if a variable is set otherwise just perform in the else part: 我有一个简单的if else场景,如果设置了变量,我需要做一些事情,否则只需在else部分执行:

if(flag) {
  doSomething();
}
doNextThing(someParam);

If flag is set, then I must doSomething() and doNextThing() 如果设置了标志,那么我必须doSomething()和doNextThing()

Both doSomething and doNextThing return promises. doSomething和doNextThing都返回诺言。 What is the best way to implement this? 实现此目的的最佳方法是什么?

Currently my code looks like: 目前,我的代码如下:

if(flag) {
  doSomething().then(doNextThing);
} 
else doNextThing(someParam);

What is the best way to elegantly combine the two in cleaner code segment? 在简洁的代码段中完美地将两者结合的最佳方法是什么?

I tried: 我试过了:

var somePromise = new Promise(function(resolve, reject){
     if(flag)
        doSomething.then(resolve(result));
      else resolve();
})

return somePromise.then(doNextThing(resolvedValue))

but this doesn't work. 但这不起作用。 Seems like the promise doSomething is never resolve and the code returns immediately. 似乎promise doSomething永远无法解决,并且代码立即返回。 I also tried several variations of the above including: 我还尝试了上述几种方法,包括:

var somePromise = new Promise(function(resolve, reject){
     if(flag)
        doSomething.then(resolve);
      else resolve();
})

and: 和:

var somePromise = new Promise(function(resolve, reject){
     if(flag)
        doSomething.then(function(result){
             resolve(result);
        }.bind(this));
      else resolve();
}.bind(this));

but nothing works, except for the very first option. 但是除了第一个选项之外,没有任何效果。 So, my question is - Is there a better/elegant way to achieve this? 因此,我的问题是-是否有更好/优雅的方法来实现这一目标?

How about something like this: 这样的事情怎么样:

Em.RSVP.allSettled([
    !flag || doSomething(),
    doNextThing()
])

It will try each item in the array, but skipping the first if the flag is false. 它将尝试数组中的每个项目,但如果标志为false,则跳过第一个项目。 If you want it to skip doNextThing() if doSomething() fails, then use Em.RSVP.all(). 如果您希望doSomething()失败,则跳过doNextThing(),请使用Em.RSVP.all()。

Try moving to Ember.RSVP.Promise and returning promise in your function (it should chain them): 尝试移至Ember.RSVP.Promise并在函数中返回promise(它应将它们链接起来):

var somePromise = new Ember.RSVP.Promise(function (resolve, reject) {
     if (flag) {
         return doSomething;
     }
     resolve();
});

return somePromise.then(doNextThing);

One elegant way of doing this: 一种优雅的方法:

var firstThing = flag ? doSomething() : Ember.RSVP.resolve();
firstThing.then(doNextThing.bind(null, someParam));

If you look in the then function I am using the bind function to create a partially applied function. 如果您查看then函数, then我正在使用bind函数创建部分应用的函数。

This is because the function then expects a function as his argument, so you can't call it like this firstThing.then(doNextThing(param)) . 这是因为该函数then一个函数作为其参数,因此不能像firstThing.then(doNextThing(param))这样调用它。 This way you are passing the result of the function, not the function. 这样,您将传递函数的结果,而不是函数的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM