我怎样才能返回一个 Promise?

释放

我正面临着 Promise 的这个问题,当我使用 ImagePicker 插件从图库中选择一张图片时,我想返回一个承诺,然后使用该文件。

这是我的代码:

takePictureFromGallery() : any {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            return new Promise((resolve, reject) => {
                                if (picSaved) resolve(filepath);
                                else reject();
                            });

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    }

这个方法属于一个叫做 PictureUploader 的类。在这门课之外,我有这个代码:

let pu = new PictureUploader();
pu.takePictureFromGallery()
    .then(s => {
        console.log(s);
})

undefined is not an object当我尝试运行该.then()方法时,我得到

有什么帮助吗?谢谢!

塞巴斯蒂安·希尔德布兰特

您需要将您的promise声明放在开头,如下所示:

takePictureFromGallery() : any {
    return new Promise((resolve, reject) => {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            if (picSaved) resolve(filepath);
                            else reject();

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    });
}

并且您还需要注意在catch中拒绝承诺(示例中未显示)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我怎样才能让这个返回 promise 的函数不阻塞其他代码?

我怎样才能返回一个循环?

我怎样才能只返回一个共同的价值?

我怎样才能牢固地返回一个结构体数组?

我怎样才能返回一个中序遍历而不是仅仅打印它

我怎样才能得到一个结果?SQL:CASE返回两行

我怎样才能适当地模拟出一个返回yield的方法?

我怎样才能重复一个模式?

我怎样才能写一个指向函数?

那我怎样才能将数据推送到Promise中的数组中呢?

我怎样才能将Promise转换为类似列表的内容?

我怎样才能聚合计数函数返回一个数字而不是一个数组

我怎样才能得到一个“枚举”的价值观在一个通用的?

我怎样才能找到的第一个犯了JGit一个分支?

我怎样才能让一个 for 语句有一个 else

我怎样才能用一个秘密密钥锁定一个javacard?

我怎样才能让一个类实现一个接口...(C ++)

我怎样才能让一个用户表单完成另一个?

我怎样才能省略一个网址,以便我到达我想要的网址

我怎样才能让Selenium使用我的firefox(而不是创建一个新的)

我怎样才能有一个接受“我的类型”类型参数的抽象方法?

我怎样才能让我的Ubuntu从给定的源中寻找一个库

我怎样才能@Input一个复杂的对象到我的AngularDart组件中?

我怎样才能让我的 CNN 输出一个特征向量

我怎样才能得到我的例子(CSS)的最后一个孩子?

我怎样才能增加一个变量,以便它存储我想要的值?

我怎样才能将两个蜘蛛组合成一个?

我怎样才能得到一个有 m 个尾巴的列表?Python

我怎样才能从2个函数中创建一个?