实现诺言模式诺言、模式

2023-09-11 23:03:57 作者:老公老婆一男一女

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

伪code:

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


          }
 

在其他应用程序我叫

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

解决方案 王者荣耀 诺言参加训练营2021,对他是赚还是亏 莫名其妙

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