Reputation: 3852
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
Upvotes: 0
Views: 553
Reputation: 46
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).
Upvotes: 2