AngularJS中的排序承诺

时间:2017-04-28 21:07:51

标签: android angularjs ionic-framework angular-promise

我试图执行以下操作:

  • 返回检查某个目录是否存在的承诺的函数(它创建的是否为“

  • 然后将文件复制到该创建的目录。

我这样做的方式,复制文件部分在promise 之前执行!所以它失败了,因为那里还没有目录!

代码: IMG_DIR_ENTRY 是控制器内的全局变量。

addImage

function addImage(imageURI) {
        return $q(function (resolve, reject) {

            handleImgDirCreation().then(function(imageDirEntry) {
                window.resolveLocalFileSystemURL(imageURI, function onResolveImgSuccess(imgFileEntry) {
                    imgFileEntry.copyTo(imageDirEntry, "img_" + + new Date(), function onCopySuccess(copiedImgFileEntry) {
                        images.push(copiedImgFileEntry.toURL());
                        window.localStorage.setItem(IMAGE_STORAGE_KEY, JSON.stringify(images));
                        resolve();
                    },
                    function onCopyFail(errMsgCopy) {
                        console.debug("ERROR: copy failed because: " + errMsgCopy);
                        reject();
                    });
                },
                function onResolveImgFail(errMsgResolveImg) {
                    console.debug("ERROR: couldn't resolve img because: " + errMsgResolveImg);
                    reject();
                });
            });
        });
    }


handleImgDirCreation (这不是在 .then()之前调用的)

function handleImgDirCreation() {
        return $q(function(resolve, reject) {

            window.resolveLocalFileSystemURL(IMAGE_DIR, function onDirExists(imageDirEntry) {
                console.debug("SUCCESS: Directorio images ya existe.");
                resolve(imageDirEntry);
            },
            function onNonExistent(errMsgNonExistent) {
                createImageDirectory("WARNING: Directory didn't exist... || " + errMsgNonExistent);
                resolve(IMG_DIR_ENTRY);
            });
        });
    }


createImageDirectory

function createImageDirectory(errMsg) {
        console.debug(errMsg);
        console.debug("Creating IMG DIR --> " + IMAGE_DIR);

        window.resolveLocalFileSystemURL(DATA_DIRECTORY, function onResolveRootSuccess(rootDirEntry) {
            rootDirEntry.getDirectory('images', {create : true}, function onCreateDirSuccess(imgDirEntry) {
                console.debug("SUCCESS: Directory created --> " + imgDirEntry.toURL());
                IMG_DIR_ENTRY = imgDirEntry;
            },
            function onCreateDirFail(errMsgCreateDir) {
                console.debug("ERROR: Couldn't create image directory because: " + errMsgCreateDir);
            });
        },
        function onResolveRootFail(errMsgResolveRoot) {
            console.log("ERROR: Couldn't resolve root directory because: " + errMsgResolveRoot);
        });

    }

重要 我是用异步承诺来做的,因为否则Android的主线程(UI线程)会被重载和阻塞并跳过帧。也许还有另一种我不知道的方法。

1 个答案:

答案 0 :(得分:1)

要让代码等待创建目录,createImageDirectory函数需要返回一个承诺:

function createImageDirectory(errMsg) {
    var future = $q.defer();
    console.debug(errMsg);
    console.debug("Creating IMG DIR --> " + IMAGE_DIR);

    window.resolveLocalFileSystemURL(DATA_DIRECTORY, function onResolveRootSuccess(rootDirEntry) {
        rootDirEntry.getDirectory('images', {create : true}, function onCreateDirSuccess(imgDirEntry) {
            console.debug("SUCCESS: Directory created --> " + imgDirEntry.toURL());
            IMG_DIR_ENTRY = imgDirEntry;
            //RESOLVE with imgDirEntry;
            future.resolve(imgDirEntry);
        },
        function onCreateDirFail(errMsgCreateDir) {
            console.debug("ERROR: Couldn't create image directory because: " + errMsgCreateDir);
            //REJECT with error
            future.reject(errMsgCreateDir);
        });
    },
    function onResolveRootFail(errMsgResolveRoot) {
        console.log("ERROR: Couldn't resolve root directory because: " + errMsgResolveRoot);
        //REJECT with error
        future.reject(errMsgResolveRoot);
    });
    //RETURN promise
    return future.promise;

}

然后handleImgDirCreation函数需要解决该承诺:

function handleImgDirCreation() {
    return $q(function(resolve, reject) {

        window.resolveLocalFileSystemURL(IMAGE_DIR, function onDirExists(imageDirEntry) {
            console.debug("SUCCESS: Directorio images ya existe.");
            resolve(imageDirEntry);
        },
        function onNonExistent(errMsgNonExistent) {
            //createImageDirectory("WARNING: Directory didn't exist... || " + errMsgNonExistent);
            //resolve(IMG_DIR_ENTRY);
            var msg = "WARNING: Directory didn't exist... || " + errMsgNonExistent
            var directoryPromise = createImageDirectory(msg)
            //RESOLVE with directory promise
            resolve(directoryPromise);
        });
    });
}

通过使用目录创建承诺解析,从该承诺链接的后续方法将等待创建目录。

  

由于承诺可以通过另一个承诺(将进一步推迟其解决方案)来解决,因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能。

     

— AngularJS $q Service API Reference (Chaining Promises)