【问题标题】:how to wrap google oauth authorize callback in a promise?如何在承诺中包装谷歌 oauth 授权回调?
【发布时间】:2018-05-12 15:29:43
【问题描述】:

我采用了 google nodejs 快速入门 examples 之一作为如何进行 oauth 的模型,将其重写为类对象,以便我可以将其包含在我的更大项目中。我可以成功运行应用程序脚本 api 调用 scripts.run 并在类对象中获得有效的返回值,但不能将其返回到包含的项目。

scripts.run 包含的函数如下所示

  Goo.prototype.testAuth = function( auth ){
     var script = google.script('v1');
     var cmd = {
        auth,
        resource: { function: 'test_auth', devMode: true },
        scriptId: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
     };

     script.scripts.run(
        cmd,
        function( err, resp ){
        if( err ){ console.log('The API returned an error: ' + err);return }
        if( resp.error ){console.log('Script error message: '
                             + resp.error.details[0].error.errorMessage);}
        else {
           console.log('resp from api call', resp.response.result );
           return resp.response.result ;
        }
     });

  };

带有resp.response.result 是有问题的部分,因为它没有传递回容器中的 var response

 var Goo = require('./Goo.js');
 var goo = new Goo();
 var response = goo.test_auth();
 console.log('response to use elsewhere ',response);

众所周知,Goo类中的console.log返回一个值,而容器中的console.log返回undefined。

如果重要的话,所有的 Goo 类都这样包装

  (function(){
     var Goo = (function(){
        var Goo = function(config){
        }
        Goo.prototype.test_auth = function(){
            this.authorize( this.testAuth );
        };
        Goo.prototype.testAuth = function(){};
        Goo.prototype.authorize = function(){};
     })();
     module.exports = Goo;
  })();

我应该如何构造它以返回要在容器中使用的值?

我不清楚我是否应该尝试将 script.scripts.run 包装在一个 Promise 中,如果它已经在返回一个 Promise 并且我不知道如何等待它的返回,或者如果我' m 处理回调函数会导致错误的解决方案。感谢您提供此处的任何指导。

我正在使用 node.js 和 googleapis ^24.0.0

【问题讨论】:

  • 感谢@danh 做到了

标签: javascript node.js rest google-apps-script promise


【解决方案1】:

testAuth 函数不会返回当前编写的任何内容。人们可以像下面这样“承诺”它......

Goo.prototype.testAuth = function( auth ){
    return new Promise((resolve, reject) => {
        var script = google.script('v1');
        var cmd = {
           auth,
           resource: { function: 'test_auth', devMode: true },
           scriptId: '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        };

        script.scripts.run(
           cmd,
           function( err, resp ){
           if( err ){
               console.log('The API returned an error: ' + err);
               reject(err);
           } else if( resp.error ){
               console.log('Script error message: ' + resp.error.details[0].error.errorMessage);
               reject(resp.error);
           } else {
               console.log('resp from api call', resp.response.result );
               resolve(resp.response.result);
           }
        });
    });
};

那就这样称呼吧……

var Goo = require('./Goo.js');
var goo = new Goo();
goo.test_auth().then(response => {
    console.log('response to use elsewhere ',response);
}).catch(error => {
    console.log('error ',error);
});

抱歉缩进格式有点奇怪。 OP 使用 3 个空格来表示缩进,而不是通常的 2 或 4 个。(自我注意:下一集“硅谷”的想法)

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 2018-05-11
    • 1970-01-01
    • 2012-02-13
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多