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

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

问题描述

我一直在尝试一个代码示例,以达成我的承诺。但是我似乎无法弄清楚如何处理回调,然后再获得可调用值。



这是我正在处理的两个相关的JSBin示例。以详细的风格写成模拟烤饼干。



没有异步的Ember JS



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



纯同步示例来显示基本行为(故意使用基本对象模型)



使用异步和承诺的Ember JS



http://jsbin.com/udeXoSE/ 1 /编辑



尝试扩展第一个示例并实现方法,其中完成了延迟,并在稍后返回一个履行的承诺对象。 p>

试图理解的概念:




  • 如何正确处理承诺,特别是Ember.RSVP 。/ />>
  • 如何使用Ember.run.later方法而不是setTimeout


解决方案


如何正确处理承诺,特别是Ember.RSVP.Promise并稍后获取对象


似乎像接近有这个想法,只需要对你的jsbin做一些微小的改变来使事情工作:



首先,而不是推送承诺到你的数组,你应该将承诺的价值推到然后回调。真的在这种情况下,你根本不需要这个承诺对象。所以:

  //在promise上调用then函数
App.cookieSlowBake(cookie).then(function价值){
alert(你的慢烤饼干已经准备好吃了);
App.CookieStore.pushObject(value);
},function(value){
/ / failure
alert(烤箱里发生了什么事对不起饼干);
});

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

  var bakedCookiePromise = new Ember.RSVP .Promise(function(resolve,reject){
var bakeTime = 2000; // milliseconds
var bakedCookie = false;
Ember.run.later(cookieDough,function(){
//这里的代码将使用这个== cookieDough
cookieDough.set('deliveryStatus',烘焙后稍后传递的cookie+ bakeTime); b b b b c b c b c c b c c c i =
resolve(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.

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

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