locked
Return var from function not work ?

    Question

  • var imgData = [];
                        Windows.Storage.AccessCache.StorageApplicationPermissions.futureAccessList.getFolderAsync(token).then(function (folder) {
                            folder.getFilesAsync().then(function (images) {
                                $.each(images, function (key, obj) {
                                    obj.properties.getImagePropertiesAsync().then(function (imgDetail) {
                                        var imgWidth = imgDetail.width;
                                        var imgHeight = imgDetail.height;
                                        var imgBlob = URL.createObjectURL(obj, { oneTimeOnly: false });
                                        imgData.push(imgWidth + imgHeight + imgBlob);
                                    });
                                });
                            }).done(function () {
                                var slider = $("#royalSlide").royalSlider({
                                    keyboardNavEnabled: true,
                                    imageAlignCenter: false,
                                }).data('royalSlider');
                            });
    // imgData null why ? 
    // why var slider run before $.each not complete
                        });

    May i stop work on winjs with this shit ?
    Wednesday, November 26, 2014 5:49 PM

Answers

  • Hi,

    As DeividKamuii hinted, you've got an issue with your Promise chaining. Promises represent async operations and you need to keep track of them if you're interested in doing something after all that async work is done. The file processing you're doing is not part of the Promise chain since you're not collecting the Promises and are missing return's there, therefore the .done() is called while file properties gathering is still ongoing. I'd propose you to carefully collect all the file handling promises to an array and using WinJS.Promise.join() to return the result of that Promise chain phase to .done(), where you then can be sure all files have been processed (and you have full set of imgData in there).

    • Marked as answer by nhattuanbl Friday, November 28, 2014 12:55 PM
    Thursday, November 27, 2014 7:05 PM

All replies

  • Are you targetting Windows 8.1 or Windows Phone 8.1?

    Have you check this:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh700334.aspx

    Wednesday, November 26, 2014 8:11 PM
  • Hi,

    As DeividKamuii hinted, you've got an issue with your Promise chaining. Promises represent async operations and you need to keep track of them if you're interested in doing something after all that async work is done. The file processing you're doing is not part of the Promise chain since you're not collecting the Promises and are missing return's there, therefore the .done() is called while file properties gathering is still ongoing. I'd propose you to carefully collect all the file handling promises to an array and using WinJS.Promise.join() to return the result of that Promise chain phase to .done(), where you then can be sure all files have been processed (and you have full set of imgData in there).

    • Marked as answer by nhattuanbl Friday, November 28, 2014 12:55 PM
    Thursday, November 27, 2014 7:05 PM