我怎样才能归还承诺?

时间:2018-06-04 15:20:01

标签: angular angular2-nativescript nativescript-plugin

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

这是我的代码:

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()

有任何帮助吗?谢谢!

2 个答案:

答案 0 :(得分:2)

您需要将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块中拒绝承诺(示例中未显示)

答案 1 :(得分:1)

我建议你返回一个可观察的。

你可以在这里阅读它 Promise vs Observable

实现与此相同的原因

takePictureFromGallery() : observable<any> {
return new observable((o) => {
    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){
                           o.next(filepath);
                           o.complete();
                          }   
                        else o.error();

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

});
}