Ai
2 Star 6 Fork 2

ereddate/ptemplatejs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ptemplate.extend.promise.js 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
ereddate 提交于 2018-12-05 09:43 +08:00 . no commit message
/*!
* ptemplatejs v1.0.0
* @author yandong
*
* https://github.com/ereddate/ptemplatejs
*/
'use strict';
typeof window.pTemplate != "undefined" && (function(win, $) {
Array.prototype.del = function(num) {
this.splice(num, 1);
return this;
};
class PP {
constructor(fn) {
var that = this;
that.resolveCallbacks = null;
that.status = "pending";
that.value = null;
that.ErrValue = null;
that.then = (resolve, reject) => {
if (this.status === "pending") {
resolve && (this.resolveCallbacks = resolve);
reject && (this.rejectCallback = reject);
} else if (this.status === "rejected") {
return reject && reject(this.Error) || this;
} else {
try {
return resolve && resolve(this.value) || this;
} catch (e) {
this.status = "rejected";
this.Error = e;
return reject && reject(this.Error) || this;
}
}
return this;
}
that.always = (callback) => {
if (this.status === "pending") {
callback && (this.alwaysCallback = callback)
} else {
this.status = "completed";
return callback && callback(this.value) || this;
}
return this;
}
that.wait = (time, resolve, reject) => {
var that = this;
setTimeout(() => {
that.then(resolve, reject);
}, time);
return this;
}
fn((args) => {
that.value = args;
that.status = "fulfilled";
try {
that.resolveCallbacks && that.resolveCallbacks(that.value);
} catch (e) {
that.status = "rejected";
that.Error = e;
that.rejectCallback && that.rejectCallback(that.Error);
}
}, (args) => {
that.status = "rejected"
that.Error = args;
that.rejectCallback && that.rejectCallback(that.Error);
});
return this;
}
}
class When {
constructor() {
var args = arguments,
len = args.length,
that = this;
that.cbs = [].slice.call(args);
return this;
}
then(resolve, reject, alwaysFn) {
this.cbs.forEach((cb) => {
new PP(cb).then((a) => {
resolve && resolve(a);
}, (e) => {
reject && reject(e)
}).always((a) => {
alwaysFn && alwaysFn(a)
});
});
return this;
}
}
$.promise = (callback) => {
return new PP(callback);
};
$.promise.when = () => {
return new When.apply(this, arguments);
};
})(window, pTemplate);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ereddate2017/ptemplatejs.git
git@gitee.com:ereddate2017/ptemplatejs.git
ereddate2017
ptemplatejs
ptemplatejs
master

搜索帮助