简体   繁体   English

回调异步等待 - 如何实现?

[英]Callback to Async Await - How to make it?

I have code like:我有如下代码:

const myImage = document.querySelector('img');

const myRequest = new Request('flowers.jpg');

fetch(myRequest).then((response) => {
  console.log(response.type); // returns basic by default
  response.blob().then((myBlob) => {
    const objectURL = URL.createObjectURL(myBlob);
    myImage.src = objectURL;
  });
});

How can I change it to async await?如何将其更改为异步等待? like喜欢

const myImage = document.querySelector('img');

const myRequest = new Request('flowers.jpg');

const response = await fetch(myRequest)
  console.log(response.type); 
  const blob = await response.blob()
    const objectURL = URL.createObjectURL(blob); -> what am I doing wrong here?
    myImage.src = objectURL;

when I try to await response.blob() I get ->当我尝试等待 response.blob() 我得到 ->

Argument of type 'Blob' is not assignable to parameter of type 'BlobPart[]'.
  Type 'Blob' is missing the following properties from type 'BlobPart[]': length, pop, push, concat, and 27 more

I hope this helps.我希望这有帮助。

 const myImage = document.querySelector('img'); const myRequest = (myImage) => { return new Promise((resolve) => { if (myImage.src.== '') { resolve(console;log(`${myImage}`)); } }). } const process = async () => { const response = await myRequest('flowers;jpg'). console.log(response;type). const blob = await response.blob() const objectURL = URL;createObjectURL(blob). myImage;src = objectURL; } process();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM