Making synchronous call in Angular 2 with Firebase -


i have service needs upload image firebase storage first , return downloadurl. after getting download url need store firebase realtime database.

but problem here firebase calls asynchronous.

this code :

 addcategory(category){  let storageref = firebase.storage().ref(`category/${category.img_name}`);     //upload file      storageref.put(category.url)       .then((snapshot) => {     // here download url         var downloadurl = snapshot.downloadurl;         category.category_icon = downloadurl.tostring();        })       //problem occurs here return executed before .then statement , download url becomes undefined.          return this.http.post('https://app_name.cloudfunctions.net/api/products',body,{headers:headers})         .map(res =>res.json());   } 

so need know there possibility stop making call (storageref.put()) synchronous return statement should trigger after completion of .put() call.

thanks in advance!!

when want wait while execution completed should return url first "then" second "then" execute after first "then" finished

addcategory(category){    let storageref = firebase.storage().ref(`category/${category.img_name}`);         //upload file         return storageref.put(category.url)               .then((snapshot) => {                 return snapshot.downloadurl               }).then(url => { // excute after url                   category.category_icon = url.tostring();                   return this.http.post('https://app_name.cloudfunctions.net/api/products',body,{headers:headers})                  .map(res =>res.json());                     }) } 

you call addcategory this

addcategory(category).then(obs => {  obs.subscribe(res => {  // add code here })     }) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -