我怎样才能 return 一个承诺?

How can I return a Promise?

我正面临 Promise 的这个问题,我想 return 当我使用 ImagePicker 插件从图库中 select 一张图片,然后处理文件时 return 一个承诺。

这是我的代码:

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 的 class。在这个 class 之外我有这个代码:

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

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

有什么帮助吗?谢谢!

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

我建议你 return 一个 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"});
        });

});
}