承诺的轻量级实现 [英] Lightweight implementation of promises

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

问题描述

我正在构建一个简单的图表工具.当用户选择数据源和图表类型时,特定图表 (.js) 的数据和脚本都会被加载,然后代码呈现图表.

I am building a simple charting tool. When a user chooses a data source and a chart type, both the data and the script for the specific chart (.js) are loaded and then the code renders the chart.

目前我使用以下顺序:

  1. 加载图表脚本
  2. 加载数据
  3. 渲染图表

为了提高性能,我想实现 promise 并并行加载脚本和数据.像 jQuery 这样的库对于我的需求来说太大了(问题的重点不是争论这个),有没有更轻量级的解决方案?也许是一个 1-2 kb 的库,或者一个教程?同样,这是一个非常基本的实现,只有两个并行操作.

To improve performance, I'd like to implement promises and load script and data in parallel. A library like jQuery is too big for my needs (the point of the question is not to argue on this), are there more lightweight solutions? Maybe a 1-2 kb library, or a tutorial? Again, this is a very basic implementation with just two actions in parallel.

[更新] 我赞成所有的回复,因为它们看起来都很棒.我会在做更多测试后回来报告.

[Update] I upvoted all the replies as they all look excellent. I'll report back after I do some more testing.

推荐答案

我不久前构建了类似的东西,我称之为when-then".我想要这样的东西:加载所有这些东西,然后在它们完成后做一些事情".

I built something like this a while back that I called "when-then". I wanted something that would say, "Load all of these things, then do something when they're done".

https://github.com/geuis/when-then

我受到 promises 想法的启发,但想要一些更简单的东西来处理简单的任务.

I was inspired by the promises idea, but wanted something a little simpler for simple tasks.

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

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