0

I have this function to upload file after the file is uploaded it returned uploaded path then i pass it to tiny url this.tinyUrl.shorten(data.url).subscribe(sUrl => { shortUrl=sUrl;}); but the issue is sUrl is returned with certain delay and further code gets executed i want till sUrl is not returned the next code should not be executed.

handleUpload(file) {
    let promise = new Promise((resolve, reject) => {
      const contentType = file[0].type;
      const bucket = new S3(
        {
          .....
        }
      );
      .....
      bucket.upload(params, (err, data)=> {
        if (err) {
       
          return false;
        } else {
          if (data.url) {
            this.tinyUrl.shorten(data.url).subscribe(sUrl => {
              shortUrl=sUrl;
            });
          }
          resolve(this.FileUploadImage);
        }
      });
      setTimeout(() => {                        
       
      }, 4000);
    });
 

  }

Any solution Thanks

5
  • Try to resolve in subscribe when url is ready. Commented Aug 18, 2022 at 11:38
  • @Taras Can you post it in Answer with example Commented Aug 18, 2022 at 11:40
  • does it work for you correct? or you want more details provided? Commented Aug 18, 2022 at 11:48
  • @Taras Still not working please post your code so that i can try Commented Aug 18, 2022 at 11:58
  • Not sure if it is answer, but try this.tinyUrl.shorten(data.url).subscribe(sUrl => {shortUrl=sUrl; resolve(this.FileUploadImage);}); Commented Aug 18, 2022 at 11:59

1 Answer 1

2

If you subscribe to something, its asynchronous. So everything outside of the subscribe will be executed, like you experienced. If you want something to happen after the subscription returned a value, it needs to be done inside the subscription, for example like Taras did:

this.tinyUrl.shorten(data.url).subscribe(sUrl => {
  shortUrl=sUrl; 
  this.FileUploadImage();
});
this.nextMethod();

this.FileUploadImage() will be executed when the subscription received a return. this.nextMethod(); will be instantly executed, after the subscription starts.

Keep in mind, that subscribing like this is deprecated (Subscribe is deprecated: Use an observer instead of an error callback).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.