简体   繁体   中英

resolve after new Promise did nothing (console.log -> undefined)

here is my Promise Function, I go through each blob in Azure BlobStorage, then I read each blob. console.log(download) delivers the values as JSON.

But to close the return new Promise function, I want that resolve should return the JSON data from the reading the blobstream. But here in my case, resolve leads to nothing.

In Angular Service.ts file the code looks like this:

getData(): Promise<JsonData[]> {

    return new Promise(async resolve => {

      const containerName = "blobcontainer";
      const containerClient = this.blobServiceClient.getContainerClient(containerName);

      //list blobs
      let i = 1;

      async function main() {
        i = 1;
        for await (const blob of containerClient.listBlobsFlat()) {

          console.log(`Blob ${i++}: ${blob.name}`);
          const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
          //console.log(blockBlobClient)
          const downloadBlockBlobResponse = await blockBlobClient.download(0);
          const download = await blobToString(await downloadBlockBlobResponse.blobBody)
          //console.log(downloadBlockBlobResponse)
          console.log(download)

        }

      }

      async function blobToString(blob: Blob): Promise<string> {
        const fileReader = new FileReader();
        return new Promise((resolve, reject) => {
          fileReader.onloadend = (ev: any) => {
            JSON.parse(ev.target!.result)
            resolve(JSON.parse(ev.target!.result));
          };
          fileReader.onerror = reject;
          fileReader.readAsText(blob);
        });
      }
      const _blob = await main().catch((err) => {
        console.error('message'); return null
      });

      resolve(_blob)  //resolve should return downloaded JSON file, but it didn't


    })
  }

Then in the component file, I want to retrieve the data from the resolve, which should return the JSON string variables like "name", "timestamp", "value"- But in my case, you receive metadata from the blob and not the contents. Means the service.ts file isn't correctly programmed:

xy.component.ts

export class xyComponent implements OnInit {
  @Input() title: string;
  //jsondatas: Array<JsonData> = [];
  jsondata: JsonData;
  name: String;
  timestamp: string;
  value: number;


  //constructor() { }

  private jsonlistService: JsonDataService;
  jsondatas: JsonData[]=null;

  constructor(private jsonService: JsonDataService) {
    this.jsonlistService = jsonService;
  }

  ngOnInit(): void {

    this.jsonlistService.getData()
      .then(results => this.jsondatas = results);

      console.log(this.jsonService)

  }

  

}

EDIT: Even if I return download at the main function, resolve from main() doesn't deliver the json string.

Second EDIT: here is the snippets how to return data:

async function main() {
        i = 1;
        for await (const blob of containerClient.listBlobsFlat()) {

          console.log(`Blob ${i++}: ${blob.name}`);
          const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
          //console.log(blockBlobClient)
          const downloadBlockBlobResponse = await blockBlobClient.download(0);
          const download = await blobToString(await downloadBlockBlobResponse.blobBody)
          
          //console.log(downloadBlockBlobResponse)
          console.log(download)
          return download
        }

      }

But I didn't receive the downloaded file, error is still the same. Would be very nice if you could help me

You doesn't return anything from main . Just return the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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