Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Ember函数转换为使用ES2017异步/等待_Javascript_Ember.js_Ember Data_Ecmascript 2017 - Fatal编程技术网

Javascript 将Ember函数转换为使用ES2017异步/等待

Javascript 将Ember函数转换为使用ES2017异步/等待,javascript,ember.js,ember-data,ecmascript-2017,Javascript,Ember.js,Ember Data,Ecmascript 2017,我想将此余烬路由操作转换为使用ES2017 async/await。有人能解释一下这是什么样子吗 根据规范,我已经在我的ember-cli-build.js文件中添加了:babel:{includePolyfill:true}: save() { let tenant = this.modelFor(this.routeName).tenant; let app = this.modelFor(this.routeName).app; return tena

我想将此余烬路由操作转换为使用ES2017 async/await。有人能解释一下这是什么样子吗

根据规范,我已经在我的ember-cli-build.js文件中添加了:babel:
{includePolyfill:true}

save() {
      let tenant = this.modelFor(this.routeName).tenant;
      let app = this.modelFor(this.routeName).app;

      return tenant.save().then(()=> {
        return tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }}).then(() => {
          this.transitionTo('tenants.tenant.info', tenant.id);
        }).catch((error) => {
          tenant.get('app').rollback();
          throw error;
        });
      }).catch((error) => {
        tenant.rollback();
        throw error;
      });
    }

您的代码已转换为异步/等待:

async save() {
    let tenant = this.modelFor(this.routeName).tenant;
    let app = this.modelFor(this.routeName).app;

    try {
        await tenant.save();

        try {
            await tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }});
            this.transitionTo('tenants.tenant.info', tenant.id);
        } catch (error) {
            tenant.get('app').rollback();
            throw error;
        }
    } catch (error) {
        tenant.rollback();
        throw error;
    }
}
要从承诺转换,需要将
wait
关键字添加到返回承诺的方法调用中。您在
中放置的所有内容,然后可以在
等待
语句之后简单地放置承诺的方法


Promissions'catch
方法转换为常规的try/catch块。

与Patrick Hund所写的答案非常相似,但将catch语句附加到等待的承诺上,而不是包装在try/catch块中,并将错误逻辑提取到单个函数中

async save() {
    let tenant = this.modelFor(this.routeName).tenant;
    let app = this.modelFor(this.routeName).app;

    await tenant.save().catch(handleError.bind(this, false));
    await tenant.get('app').save({ adapterOptions: { tenantId: tenant.id }})
                .catch(handleError.bind(this, true));

    this.transitionTo('tenants.tenant.info', tenant.id);

    // isApp is bound via bind; error will be provided when called via catch        
    function handleError(isApp, error) {
        if (isApp) {
            tenant.get('app').rollback();
        } else {
            tenant.rollback();
        }
        throw error;
    }
}

更新:刚刚了解到,截至2018年6月2日,Ember在代码中不支持async/await。只有在测试中。余烬并发是下一个最好的选择。这是不正确的。您可以使用async/await。在某些情况下,仅仅测试是一个问题。到底是什么不起作用?这是正确的@Lux,您应该暂时避免使用async/Wait应用程序内代码,除非您使用的是babel polyfill,否则使用的是本机promise实现,并且它不支持运行循环,下面最好捕获对话。Tl;dr现在使用
ember并发
ember co
。已删除上述注释所涉及的自动运行断言: