此函数尝试加载图像,如果未加载图像,则再次调用该函数:
function loadImage (url = '', callback = () => {}) {
utils.loadImage(url, () => {
callback()
}, () => {
loadImage(url, callback)
})
}
我希望它能返回,所以我写道:
function loadImage (url = '', callback = () => {}) {
return new Promise((resolve, reject) => {
utils.loadImage(url, () => {
// what to do with resolve and reject
callback()
}, () => {
loadImage(url, callback)
})
})
}
但是我不确定在哪里解决和拒绝...
最佳答案
const loadImage = url => new Promise(resolve => {
utils.loadImage(url, resolve, () => {
loadImage(url).then(resolve)
})
})
编辑:更好的版本,感谢贝尔吉的评论:
const loadImage = url => new Promise(resolve => {
utils.loadImage(url, resolve, () => resolve(loadImage(url)))
})