如何创建承诺树? [英] How to create a tree of promises?

查看:81
本文介绍了如何创建承诺树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Ember中创建一个承诺树。

I'm trying to create a tree of promises in Ember.

        return this.store.find('session', 'session').then(function(session) {
            if (session.get('isEmpty')) {
                return this.store.createRecord('session').save().then(function(session) {
                    session.set('id', 'session');

                    return session.save();
                }.bind(this));
            } else {
                return session;
            }
        }.bind(this), function(session) {
            return this.store.createRecord('session').save().then(function(session) {
                session.set('id', 'session');

                return session.save();
            }.bind(this));
        }.bind(this)).then(function(session) {
            this.controllerFor('application').onLanguageChange();

            this.set('localStorage.session', session);

            return session;
        }.bind(this));

我想执行如下所示的承诺。提到还有嵌套的promise createRecord(..)。save()。then 。有可能这样做吗?

I would like to execute the promises as shown. Mention that there are also nested promises createRecord(..).save().then. Is it possible to do that?

这里并不是一棵许诺树,因为应该对两个分支都执行最后一个。如果我将它们放在自己的函数中,当然可以。像这样:

It's not exactly a tree of promises here since the last one should be executed for both branches. It could be of course if I put those in an own function. So like this:

'successBranch'.then(function(session) {
   setSessionDependents(session);
   return session;
}

'failBranch'.then(function(session) {
   setSessionDependents(session);
   return session;
}

function setSessionDependents(session) {
    this.controllerFor('application').onLanguageChange();

    this.set('localStorage.session', session);
}


推荐答案


应该对两个分支都执行最后一个

the last one should be executed for both branches

实际上确实如此!如果错误处理程序没有抛出异常,错误已经得到处理,并且promise确实通过处理程序的 return 值来解决。

It does actually! If an error handler doesn't throw an exception, the error had been handled and the promise does resolve with the return value of the handler.


有可能这样做吗?

Is it possible to do that?

是的! then 的核心属性的一部分,它使用嵌套的舞会解析。

Yes! That's one of the core properties of then, that it resolves with nested promises.

但是,您可以稍微简化一下代码,因为其中有很多重复项:

However, you could simplify your code a little bit, as you've got a bunch of duplication in there:

return this.store.find('session', 'session').then(function(session) {
    if (session.get('isEmpty')) {
        throw new Error("no session found");
    else
        return session;
}).then(null, function(err) {
    return this.store.createRecord('session').save().then(function(session) {
        session.set('id', 'session');
        return session.save();
    });
}.bind(this)).then(function(session) {
    this.controllerFor('application').onLanguageChange();
    this.set('localStorage.session', session);
    return session;
}.bind(this));

这篇关于如何创建承诺树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆