【发布时间】:2020-09-19 03:16:47
【问题描述】:
我查看了许多实现,它们看起来都非常不同,我无法真正提炼出 Promise 的本质。
如果我不得不猜测它只是一个在回调触发时运行的函数。
有人可以在几行代码中实现最基本的承诺吗?
例如来自这个answer
片段 1
var a1 = getPromiseForAjaxResult(ressource1url);
a1.then(function(res) {
append(res);
return a2;
});
传递给then的函数如何知道何时运行。
也就是说,它是如何传递回 ajax 完成时触发的回调代码的。
片段 2
// generic ajax call with configuration information and callback function
ajax(config_info, function() {
// ajax completed, callback is firing.
});
这两个 sn-ps 有什么关系?
猜测:
// how to implement this
(function () {
var publik = {};
_private;
publik.then = function(func){
_private = func;
};
publik.getPromise = function(func){
// ??
};
// ??
}())
【问题讨论】:
标签: javascript