Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a Promise to Observable

trying to convert swal's first promise function to observable and trying to use the cancel action. Can someone help me with the syntax pls.

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, cancel!',
  confirmButtonClass: 'btn btn-success',
  cancelButtonClass: 'btn btn-danger',
  buttonsStyling: false
}).then(function () {
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  )
}, function (dismiss) {
  // dismiss can be 'cancel', 'overlay',
  // 'close', and 'timer'
  if (dismiss === 'cancel') {
    swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )
  }
})

So far I have the following:

export class DialogService {
    confirm(title: string, message: string):Observable<any> {
        return Observable.fromPromise(swal({
            title: title,
            text: message,
            type: 'warning',
            showCancelButton: true
        }));
    }; }

How do I add the function (dismiss) function in the above?

like image 511
Anand Rockzz Avatar asked Feb 04 '23 19:02

Anand Rockzz


1 Answers

I'm not sure that swal use native Promise api, I think it uses A promise library for JavaScript like q or something else.

export class DialogService {
    confirm(title: string, message: string):Observable<any> {
       return Observable.create( (observer: any) => {
                 swal({
                   title: title,
                   text: message,
                   type: 'warning',
                   showCancelButton: true
                 }).then((data)=>{
                    observer.next(data);
                 },(reason)=>{
                    observer.error(reason);
                 })
            })
     }
}
like image 165
El houcine bougarfaoui Avatar answered Feb 07 '23 09:02

El houcine bougarfaoui