draw: async function (json_exe, parentId, json_nowTypeset) {
//答题区
//根据答案自动生成高度
let answer_height = 0
let img_number = 0
if (json_exe.ExeInfo.SubjectiveAnswer) {
for (let i = 0; i < answer_div.children.length; i++) {
//是否存在图片,如果存在则获取图片的高度
let answer_img = answer_div.children[i].getElementsByTagName("img")
if (answer_img.length === 1) {
img_number += answer_img.length
let img = new Image()
img.src = answer_img[0].src
answer_height -= this.subject_row_height
img.onload = async function () {
img_number--
answer_height += img.height
return answer_height
}
}
if (answer_img.length > 1) {
answer_height += 60
}
}
}
} else {
answer_height = this.subject_default_height
}
//是否小于默认高度
if (answer_height < Subjective.subject_default_height) {
answer_height = Subjective.subject_default_height
}
let str_result +=
'<div class="hex-exe-end" ' +
'exercise-id="' + json_exe.ExeInfo.ExerciseId + '" ' +
'exercise-parent-id="' + parentId + '" ' +
'style="height: ' + answer_height + 'px" ' +
'exercise-type="' + exerciseType + '"></div>';
return str_result
},
这个差不多就是这个方法,之后我在其他js里调用了这个draw,我想用它返回的那个div,
str_insertHTML += xxxx.draw(oneExe, '', this.json_nowTypeset)
但是当我使用.then
的时候它提示.then
为为解析的函数或者变量
我想问两个问题
1、我想知道我这个async和await的写法是否正确
2、为什么.then
会提示这个
你这个await async 关键字怎么还能连一起使用的?
好乱啊 你这个 await async
await 后面的函数需要是promise函数才行 .then也一样 promise函数后面才能.then
你是想调用是 加载图片 加载完放回 图片的高度啥的吧
那就 这个draw 直接 return promise 就行 把你原来写的全部放到promise里面 然后图片onload 里面 resolve出你要返回的信息
我看你draw方法 里面好像也不需要用到 async/await吧
await 是用来接收promise 函数的 async 是用来声明 这个函数需要用到 await
而.then 则是需要promise函数才能.then
没有比这 更乱的写法了。
18行 定义了一个函数getImg
,执行draw时,根本没有执行这个函数,不知道写这个函数的意义何在
.then
报错是因为你只是定义了异步函数,并没有使用Promise
可以看一下这篇文章,async使用方法,前几天总结的Peomise和async使用方法,都有简单的例子,希望对你有帮助