实现诺言模式 [英] Implement promises pattern

查看:157
本文介绍了实现诺言模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数调用与其他物体,当我的应用程序加载时,得到的参数和功能,应该调用等待所有对象,然后用承诺的模式执行,所以我可以保证所有的对象被加载,但我不'知道有关对象的数量,我不想设置超时loading.I不谈论具体的技术,如的jQuery ,需要算法。

伪code:

 函数loadWidget(ID){
        list.push(ID);
       //这里我要确保所有Widget是装


          }
 

在其他应用程序我叫

  app.loadWidget.add(widget1.id);
    。
    。
    。
    app.loadWidget.add(widget2.id);
 

解决方案

如果你不想准备好解决方案,而是要实现所有的算法,那么你自己阅读维基百科的文章中所引用的论文:的期货及承诺并的规格的承诺/ A 承诺/ A +

和确保你正在做的是正确的,读的缺少承诺通过Domenic Denicola 中的点,看到了符合性试验的承诺/ A +

如果,在另一方面,你决定,这是不值得重新发明轮子(除非是教育 - 在这种情况下,通过各种手段重塑尽可能多轮越好),那么就来看看的 RSVP.js 中,一个轻量级库,提供工具管理异步code。 RSVP.js由 Ember.js

用于例如

I have a function that called with other objects and when my application is loaded, get parameter and the function should wait for call with all of object and then execute so with promises pattern i can ensure all object is loaded but i don't know about count of object and i don't want set timeout for loading.I don't talk about specific technology such as jquery and need algorithm.

Pseudo Code:

      function loadWidget(id){
        list.push(id);
       //here I should ensure all Widget is loaded


          }

in other application i call

    app.loadWidget.add(widget1.id);
    .
    .
    .      
    app.loadWidget.add(widget2.id);

解决方案

If you don't want ready solutions and instead you want to implement all of the algorithms yourself then read the papers referenced in the Wikipedia article: Futures and promises and the specs for Promises/A and Promises/A+.

And to make sure that you're doing it right, read You're Missing the Point of Promises by Domenic Denicola and see the Compliances tests for Promises/A+.

If, on the other hand, you decide that it is not worth it to reinvent the wheel (unless it is for education - in which case by all means reinvent as many wheels as possible) then take a look at RSVP.js, a lightweight library that provides tools for organizing asynchronous code. RSVP.js is used for example by Ember.js.

这篇关于实现诺言模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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