由于低版本Qt不支持JavaScript的Promise对象,所以这里用一个轻量级Promise的例子列举一下。
原版代码路径:https://github.com/stackp/promisejs
这里我们进行了一下裁剪,去除网络请求的部分,新的代码如下:
// promise.js
(function(exports) {
function Promise() {
this._callbacks = [];
}
Promise.prototype.then = function(func, context) {
var p;
if (this._isdone) {
p = func.apply(context, this.result);
} else {
p = new Promise();
this._callbacks.push(function () {
var res = func.apply(context, arguments);
if (res && typeof res.then === 'function')
res.then(p.done, p);
});
}
return p;
};
Promise.prototype.done = function() {
this.result = arguments;
this._isdone = true;
for (var i = <