Javascript 如何在另一个函数中解析异步函数?
你好,我有这个功能:Javascript 如何在另一个函数中解析异步函数?,javascript,reactjs,ecmascript-6,async-await,Javascript,Reactjs,Ecmascript 6,Async Await,你好,我有这个功能: const getImageSize = (url) => { const img = new Image() img.src = url return img.onload = async function() { return{ height: this.height, width: this.width, scale: parseInt(this.height
const getImageSize = (url) => {
const img = new Image()
img.src = url
return img.onload = async function() {
return{
height: this.height,
width: this.width,
scale: parseInt(this.height / this.width)
}
}
}
我这样称呼它
const test = async () =>{
const imageSize = await getImageSize(data[0].image.url)
console.log(imageSize)
}
我得到的是函数本身,但不是值。
我怎样才能解决这个问题
这是日志:
ƒ () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep…
async
只是Promise
的语法糖。在这种情况下,您可以返回如下承诺:
const getImageSize=(url)=>{
常量img=新图像()
img.src=url
让我们解决
img.onload=函数(){
决心({
高度:这个高度,
宽度:这个。宽度,
比例:parseInt(this.height/this.width)
})
}
返回新承诺(r=>resolve=r)
}
您需要使用明确的承诺(new promise
)而不是async
关键字来解决此问题。thx这是有效的。