【发布时间】:2015-08-15 02:55:42
【问题描述】:
我正在阅读有关如何使用 Promise 的文档,并且经常将“resolve”和“reject”作为参数传递给 Promise 构造函数,即使没有人定义过“resolve”或“reject”函数。这怎么可能?难道我们不需要在使用之前定义函数吗?
var p1 = new Promise(
// The resolver function is called with the ability to resolve or
// reject the promise
function(resolve, reject) {
log.insertAdjacentHTML('beforeend', thisPromiseCount +
') Promise started (<small>Async code started</small>)<br/>');
// This only is an example to create asynchronism
window.setTimeout(
function() {
// We fulfill the promise !
resolve(thisPromiseCount);
}, Math.random() * 2000 + 1000);
});
【问题讨论】:
-
在您发布的代码中,
resolve和reject是正式的函数参数。任何地方都没有通过;它是函数的定义,这些是参数的名称。当函数实际被调用时,那些将引用将在别处定义的函数。
标签: javascript constructor promise