Behavior of WinJS.Promise.Join during Exceptions

Introduction

Promise is an object, which promises the user to get back the values or do some task after performing the promised activity without hampering the main UI. The execution of the other activities will work as normal. So in single word we can say, it is asynchronous.

The JOIN method of the Promise works like a waiter in the restaurant, waiting for the whole order from a table from each person. We can club separate promise objects and join them together. Join ensures to perform a task after all the promise objects finished their execution.

Queries encountered on the above

On all the above, I encountered the query,

  • what will happen when any of the Promise gets an error ?
  • Will it able to club the output and wait for the promises ? OR it will quite the execution ? Etc. etc.

After some of the homework and trials below are my findings.

Solution tried

Trial 1

var MyPromiseObjectArray = [];

MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));

WinJS.Promise.join(MyPromiseObjectArray).done(function () {
               Windows.UI.Popups.MessageDialog("All done").showAsync();
});
Result Conclusion
Show dialog with text “All done” Join operation waits and executes after the successful execution of clubbed promise members.

Trial 2

var MyPromiseObjectArray = [];

MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "http://mindfiremobile.wordpressMAKEEXCEPTION.com/2014/05/12/toast-mytoast-an-android-feature/" }).then(

           function () {},
           function () {
                document.getElementById("lab1").innerText = "Sorry, exception occurred";
           }
));

WinJS.Promise.join(MyPromiseObjectArray).done(function () {
           Windows.UI.Popups.MessageDialog("All done").showAsync();
});

Note : “Lab1” is a lable added to the concerned HTML file. In the last XHR call, we have misspelled the word “wordpress”, which has caused the exception.

Result Conclusion
A label will appear with text “Sorry, exception occurred”

Show dialog with text “All done”

Join operation executed normally. Handling of exceptions in promise, will also be treated as successful promise execution.

Trial 3

var MyPromiseObjectArray = [];

MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "https://mindfiremobile.wordpress.com/2014/05/12/toast-mytoast-an-android-feature/" }));
MyPromiseObjectArray.push(WinJS.xhr({ url: "http://mindfiremobile.wordpressMAKEEXCEPTION.com/2014/05/12/toast-mytoast-an-android-feature/" }));
 
WinJS.Promise.join(MyPromiseObjectArray).done(function () {
              Windows.UI.Popups.MessageDialog("All done").showAsync();
});

Note : In the last XHR call, we have misspelled the word “wordpress”, which has caused the exception.

Result Conclusion
Throw exception Join execution fail, due to the unhandled exception occurred in one of the promise, which means the promise execution of clubbed members did not succeed.

Lessons learnt

  • Join operation will get executed On successful execution of the joined promises.
  • In case of any promise, has any exceptions and it has handled then the promise object execution will be treated as successful. So Join operation will execute normally.

Written By: Nirmal Hota, Cross-Platform Mobile Lead, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s