我已经开始学习javascript承诺了。但我无法理解承诺的概念。 最困扰我的是谁将Resolver和Reject函数传递给promise构造函数?
参见Promise的这个例子:
function getImage(url){
return new Promise(function(resolve, reject){
var img = new Image()
img.onload = function(){
resolve(url)
}
img.onerror = function(){
reject(url)
}
img.src = url
})
}
现在谁传递了解析和拒绝方法,因为我对javascript的理解告诉我,这个脚本会抛出未知的变量错误,因为解析和拒绝没有被定义?
getImage('doggy.jpg').then(function(successurl){
document.getElementById('doggyplayground').innerHTML = '<img src="' + successurl + '" />'
}).catch(function(errorurl){
console.log('Error loading ' + errorurl)
})
现在你看到一个像上面这样的方法,这些方法(解析和拒绝)传递的唯一方法是通过then和catch在上面的方法调用getImage中使用。
答案 0 :(得分:3)
最困扰我的是谁将Resolver和Reject函数传递给promise构造函数?
没有人。
函数通过传递 promise构造函数。
他们将传递给作为promise构造函数的第一个参数传递的函数。
答案 1 :(得分:3)
我在理解Promises时也遇到了同样的问题。您需要仔细研究诺言创建的过程。
当你写
var promise= new Promise(function(resolve,reject){...})
您实际上是在调用Promise类的构造函数或创建Promise类的对象。现在Promise构造函数需要一个函数回调。 现在,resolve和reject只是函数参数,而不是其他任何值。 您可以编写任何文件来代替resolveHandler或rejectHandler。
这些解析或拒绝不过是Promise在执行Promise时调用的函数回调。
现在,成功执行Promise时将调用resolve,而在执行Promise时出现错误或执行不成功时将调用reject。
像这样可以在then
内部访问调用解析的参数
getImage().then(function(valueSentInResolve){ console.log('')})
可以在catch
内部访问调用拒绝的参数
getImage().then(function(valueSentInResolve)
{//..do something}
).catch(function(errorSentInReject){
console.log('error',errorSentInReject )
})
我希望能对您有所帮助。让我知道我说错什么了。
答案 2 :(得分:2)
使用回调初始化Promise构造函数,构造函数在调用回调时将reject
和resolve
作为参数传递。
这是一个简单的演示:
class PromiseDemo {
constructor(cb) {
cb(this.resolve.bind(this), this.reject.bind(this));
}
resolve(d) {
console.log('resolve', d);
}
reject(d) {
console.log('reject', d);
}
}
new PromiseDemo((resolve, reject) => {
Math.random() > 0.5 ? resolve('1') : reject('1');
});
答案 3 :(得分:0)
promise库创建并传递这些函数,以及跟踪承诺和记录完成,存储状态和进度,取消它等所需的所有其他元数据。
Bluebird have published some info背后的人们关于图书馆如何在内部运作,你可以看到更多in the Bluebird source。
答案 4 :(得分:0)
这有意义吗?这种解释可能是完全不正确的!
我们提供了应该异步运行的逻辑。该逻辑应接受resolve
和reject
这两个函数。这些功能的引用由Promise
提供。当我们有最终值或错误时,我们的逻辑应调用这些函数。最初创建的Promise
处于Pending
状态。调用resolve
和reject
分别将状态更改为Fulfilled
或Rejected
。
executeFunction(res,rej) = {
do some op, say DB query.
if (success) res(value) //executeFunction uses the resolving functions to change state of the Promise. Calling res fulfills the promise with value
if (fail) rej(reason)//executeFunction uses the resolving functions to change state of the Promise. Calling rej rejects the promise with reason
}
我们创建一个Promise,而不是直接调用executeFunction
(它将使调用同步),而是将在单独的线程中executeFunction
运行let p = Promise(executeFunction(res,rej));
代码。我们获得了Promise的参考。
我的猜测是在Promise
内部,发生以下情况
Promise(e(res,rej)) = {
// the Promise's constructor creates a new promise, initially in the pending state. It calls `e` (the executeFunction function) and provides references to the resolving functions to it that can be used to change its state.
state = pending;
e(_res,_rej); //starts my op. asynchronously (a new thread). Reference to resolving functions, _res, _rej is provided. _res and _rej are Promise's internal functions (see below)
//constructor doesn't return till the async thread finishes
}
_res (value){ //Promise's internal method
//probably sets the state of promise to Fulfilled and store the result of the Promise
state = fulfilled
resolvedValue = value;
}
_rej {//Promise's internal method
probably sets the state of promise to Rejected and store the result of the Promise
state = rejected
resolvedValue = error;
}
创建Promise
会异步开始执行代码。现在,我们很想知道executeFunction
的结果是什么(我们不在乎executeFunction
何时完成)。为此,我们调用then
Promise
中的p
。 then
接受两个可选参数,并将它们注册为回调。我不确定何时和谁调用这些回调。我知道then
返回了另一个Promise
,但我无法理解其工作原理
then(executeFnIfPromiseResolved, executeFnIfPromiseRejected):Promise {
register executeFnIfPromiseResolved as callback
register executeFnIfPromiseRejected as callback
//NOT SURE WHO AND WHEN THE CALLBACKS ARE CALLED
//then needs to return Promise. What would be the executor function of that Promise?
}