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

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

问题描述

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



伪代码:

  function loadWidget(id){
list.push (ID);
//这里我应该确保所有的Widget加载


}

在其他应用程序中我调用

  app.loadWidget.add(widget1.id); 



app.loadWidget.add(widget2.id);


解决方案

如果你不想准备好解决方案,想要自己实现所有的算法,然后阅读维基百科文章中引用的论文:期货和承诺以及 Promises / A Promises / A +



要确保你'阅读你错过了Domenic Denicola的承诺点,并查看 Promises / A + 。



另一方面,如果你决定不值得重新发明轮(除非是为了教育 - 在这种情况下,尽可能多地重新创造尽可能多的车轮),然后再看一下 RSVP.js ,这是一个提供用于组织异步代码的工具的轻量级库。 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天全站免登陆