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

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

问题描述

我有一个用其他对象调用的函数,当我的应用程序加载时,获取参数,该函数应该等待所有对象的调用,然后使用承诺模式执行,我可以确保所有对象都被加载,但我不不知道对象的数量,我不想为加载设置超时.我不谈论诸如jquery和需要算法的具体技术.

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.

伪代码:

      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);

推荐答案

如果您不想要现成的解决方案,而是想自己实现所有算法,请阅读维基百科文章中引用的论文:未来和承诺 以及 Promises/APromises/A+.

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+.

并确保您做对了,请阅读 您错过了 Domenic Denicola 的承诺点,请参阅 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+.

另一方面,如果您认为重新发明轮子不值得(除非是为了教育 - 在这种情况下,无论如何都要重新发明尽可能多的轮子),然后看看 RSVP.js,一个提供用于组织异步代码的工具的轻量级库.例如,Ember.js 使用 RSVP.js.

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天全站免登陆