0

I work in durandal project.

I using the plugin dialog module.

I want to write function that show message to the user and return the dialog result.

For example:

 function isValidFunc() {

       dialog.show(myHtmlPage).then(function validDialogClosed(result) {
             return result;
       }
 }

 var isValid = isValidFunc();

In my example, function validDialogClosed return the result, but isValidFunc not return anything!

I want the isValidFunc to return the result.

Please, don't answer me to show the dialog out of the function isValidFunc. I need it to show it.

Thank.

user5260143
  • 1,048
  • 2
  • 12
  • 36

1 Answers1

0

You've two problems here.

First off isValidFunc isn't returning anything. Your return statement just returns a value from your callback function, which isn't used.

Secondly, dialog.show() returns a promise, which is asynchronous. This means isValidFunc() will return before your callback function is executed.

isValidFunc() can absolutely show the dialog, but it can't return the result directly.

What it can do, is return the promise like this and you can do something with it when it completes:

 function isValidFunc() {
     return dialog.show(myHtmlPage);
 }

 isValidFunc().then(function(dialogResult) {
    //do something with dialogResult
 });
JamesT
  • 2,988
  • 23
  • 29
  • Thank, but I know it before I asked, too. I wanted to know if there is any way to do it. Use the promise - is the way that I used at another places in my projects. Here, I need it exactly as I described. But thank very match. – user5260143 Apr 03 '14 at 08:55
  • I don't believe you can wait for a promise to complete before returning from your function. Your question isn't clear that you are asking for that with full understanding of the 'proper' way of doing things. You might want to try and reword it. Although I'd say you might want to rethink your design. – JamesT Apr 03 '14 at 10:55