试图了解灰烬JS承诺 [英] Trying to understand Ember JS promises

查看:128
本文介绍了试图了解灰烬JS承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在code例如工作,让我的头周围的诺言。但我似乎无法弄清楚如何处理回调并获得了thenable价值更高版本。

下面是两个相关的例子JSBin我的工作。写在冗长的风格模仿的曲奇。

Ember公司JS无异步

http://jsbin.com/iSacev/1/edit

纯粹的同步例子来说明的基本行为(故意使用基本对象模型)

Ember公司JS与异步和承诺

http://jsbin.com/udeXoSE/1/edit

尝试扩展的第一个例子和实施方法,其中的东西将被延迟进行,后来在时间返回履行诺言的对象。

概念试图了解:


  • 如何妥善处理的承诺,特别Ember.RSVP.Promise,后来得到一个对象。

  • 如何使用Ember.run.later方法​​代替的setTimeout


解决方案

  

如何妥善处理的承诺,特别Ember.RSVP.Promise,后来得到一个对象


好像你接近有这个想通了,就不得不做出一些小的改动你的jsbin把事情的工作:

第一,而不是推的的到你的数组,你应该推的承诺传递给然后回调的价值。果然在这种情况下你不需要的诺言对象都没有。所以:

  //调用然后在承诺发挥作用
App.cookieSlowBake(饼干)。然后(函数(值){
  警报(您慢烤饼干即食);
  App.CookieStore.pushObject(值);
},功能(价值){
  //失败
  警报(出事的烤箱遗憾没有饼干。);
});

第二个变化是,以修复cookieSlowBake的错误。在原始版本的承诺正在拒绝,因为条件测试结果将总是为false,因为它在Ember.run.later回调不是的。新版本摆脱了有条件的,只是解决回调时做的承诺。

  VAR bakedCookiePromise =新Ember.RSVP.Promise(函数(解析,拒绝){
  VAR bakeTime = 2000; //毫秒
  VAR bakedCookie = FALSE;
  Ember.run.later(cookieDough,函数(){
    // $ C $其中,C将在RunLoop内执行有关bakeTime这个== cookieDough
    cookieDough.set('deliveryStatus',+ bakeTime曲奇烘焙后交付后);
    bakedCookie =真;
    解决(cookieDough);
  },bakeTime);
});

在这里看到工作jsbin: http://jsbin.com/ebOBEk/1/edit


  

如何使用Ember.run.later方法​​代替的setTimeout


他们基本上是同样的事情。你似乎正确地使用它。

I have been trying to work on a code example to get my head around promises. But I can't seem to figure out how to deal with the callbacks and get the "thenable" value later.

Here are two relevant JSBin examples I am working on. Written in verbose style to emulate baking cookies.

Ember JS without async

http://jsbin.com/iSacev/1/edit

purely synchronous example to show the basic behavior (deliberately using basic object model)

Ember JS with async and promises

http://jsbin.com/udeXoSE/1/edit

Attempt to extend the first example and implement method where things are done with a delay and returns a fulfilled promise object later in time.

Concepts attempting to understand:

  • How to properly handle promises and specifically Ember.RSVP.Promise and get an object later.
  • How to use the Ember.run.later method instead of setTimeout

解决方案

How to properly handle promises and specifically Ember.RSVP.Promise and get an object later

Seems like you are close to having this figured out, just had to make some minor changes to your jsbin to get things working:

First, instead of pushing the promise onto your array you should push the value that the promise passes to the then callback. Really in this case you don't need that promise object at all. So:

// Call the then function on the promise
App.cookieSlowBake(cookie).then(function(value) {
  alert("Your slow baked cookies are ready to eat");
  App.CookieStore.pushObject(value);
}, function(value) {
  // failure
  alert("Something happened with the oven sorry no cookies.");
});

Second change was to fix a bug in cookieSlowBake. In the original version the promise was being rejected because of a conditional test that would always evaluate to false since it was not in the Ember.run.later callback. The new version gets rid of the conditional and just resolves the promise when the callback is done.

var bakedCookiePromise = new Ember.RSVP.Promise(function(resolve, reject){
  var bakeTime = 2000; // milliseconds
  var bakedCookie = false;
  Ember.run.later(cookieDough, function() {
    // code here will execute within a RunLoop in about the bakeTime with this == cookieDough
    cookieDough.set('deliveryStatus', "cookie delivered later after baking " + bakeTime);
    bakedCookie = true;  
    resolve(cookieDough);
  }, bakeTime);
});

See working jsbin here: http://jsbin.com/ebOBEk/1/edit

How to use the Ember.run.later method instead of setTimeout

They are basically the same thing. You seem to be using it correctly.

这篇关于试图了解灰烬JS承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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