何时或谁通过解决并拒绝JS承诺的功能? [英] When or who does pass resolve and reject functions to JS promises?

查看:53
本文介绍了何时或谁通过解决并拒绝JS承诺的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习javascript承诺了。但我无法理解承诺的概念。
最困扰我的是谁将Resolver和Reject函数传递给promise构造函数?

I have begin learning javascript promises. But I just can't understand the concept of promises. The thing that bothers me most is who is passing the Resolver and Reject function to a promise constructor ?

请参阅Promise的这个示例:

See this example of 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的理解告诉我,这个脚本会抛出未知的变量错误,因为没有定义解析和拒绝?

Now who does pass resolve and reject methods , as my understanding of javascript says me that this script will throw unknown variable errors as resolve and rejects are not defined ?

getImage('doggy.jpg').then(function(successurl){
    document.getElementById('doggyplayground').innerHTML = '<img src="' + successurl + '" />'
}).catch(function(errorurl){
    console.log('Error loading ' + errorurl)
})

现在你看到一个类似上面的方法,唯一的方法传递这些方法(解析和拒绝)是通过then和catch在上面的方法中调用getImage。

Now you see a method like the above , the only way these methods(resolve and reject) are passed are via then and catch as used in above method call to getImage.

推荐答案


最困扰我的是谁将Resolver和Reject函数传递给promise构造函数?

The thing that bothers me most is who is passing the Resolver and Reject function to a promise constructor ?

没有人。

函数通过传递承诺构造函数。

The functions are passed by the promise constructor.

它们传递 to 您传递的函数作为promise构造函数的第一个参数。

They as passed to the function you pass as the first argument to the promise constructor.

这篇关于何时或谁通过解决并拒绝JS承诺的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆