Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for promises #126

Open
markwoon opened this issue Nov 11, 2016 · 4 comments
Open

Support for promises #126

markwoon opened this issue Nov 11, 2016 · 4 comments

Comments

@markwoon
Copy link

It would be nice if .confirm() and .prompt() could return a promise instead of having to provide callbacks.

@ghost
Copy link

ghost commented Feb 21, 2017

Yes! Promises highly needed!

@juliepanda
Copy link

I made a quick and dirty wrapper to make .prompt thenable. You can adapt this to confirm or any of the other alertify methods as well.
The only problem would be that you'd lose the DOM node that get passed through e because Promise doesn't allow passing multiple values through resolve.

const promisifyPrompt = (title, description, defaultValue) => new Promise((resolve, reject) => {
    alertify.prompt(
      title,
      description,
      defaultValue,
      (e, value) => resolve(value),
      reject
      );
  });

You can use it like:

promisifyPrompt(title, description, defaultValue)
.then(
  value => {/* success handler */},
  err => {/* error handler */}
);

or

promisifyPrompt(title, description, defaultValue)
.then(value => {/* success handler */})
.catch(err => {/* error handler */})

@MohammadYounes
Copy link
Owner

Thanks @juliepanda

If you need the event, simply resolve a result (e, value) => resolve({e,value}), then:

promisifyPrompt('title', 'description', 'defaultValue')
.then(({e, value}) => {/* success handler */})
.catch(err => {/* error handler */})

@mastrauckas
Copy link

mastrauckas commented Nov 20, 2017

Just thought this would help someone. I took a little different approach.

Now the below is using a confirm dialog box. It is also using typescript which can be converted to javascript faily easy:

import * as alertify from 'alertifyjs';

export enum ConfirmResult {
  Ok = 1,
  Cancel
}

export function promisifyConfirm(title: string, message: string, options = {}): Promise<ConfirmResult> {

  return new Promise<ConfirmResult>((resolve) => {
    alertify.confirm(
      title,
      message,
      () => resolve(ConfirmResult.Ok),
      () => resolve(ConfirmResult.Cancel)).set(Object.assign({}, {
        closableByDimmer: false,
        defaultFocus: 'cancel',
        frameless: false,
        closable: false
      }, options));
  });
}

Now using promisifyConfirm:

  async myFunction() {
    const confirmResult = await promisifyConfirm('My title', 'My message');
    // confirmResult wil be ConfirmResult.Ok for Ok and ConfirmResult.Cancel for Cancel
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants