Node.js node-gcloud同步调用

我使用node-gcloud https://github.com/GoogleCloudPlatform/gcloud-node与Google云端存储进行交互。

我正在开发一个node.js服务器(我的第一个node.js项目),为客户提供一小组API。 基本上,当用户上传文件时,API调用会返回签名的url来显示该文件。

getSignedUrl函数是asynchronous的https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.8.1/storage?method=getSignedUrl ,我找不到从另一个函数返回结果的方法。

我已经开始玩蓝鸟的承诺,但是我不能明白。 这是我的代码:

var _signedUrl = function(bucket,url,options) { new Promise(function (resolve, reject) { var signed_url bucket.getSignedUrl(options, function(err, url) { signed_url = err || url; console.log("This is defined: " + signed_url) return signed_url }) }) } var _getSignedUrl = function(url) { new Promise(function(resolve) { var options = config.gs , expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14) , bucket = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs }) , signed_url = null options.action = 'read' options.expires = expires// 2 weeks. options.resource= url signed_url = resolve(_signedUrl(bucket,url,options)) console.log("This is undefined: " + signed_url) return JSON.stringify( {url: signed_url, expires: expires} ); }); } 

我想我错过了它应该如何工作的基本知识,所以任何提示将不胜感激。

编辑:

对于第一条评论,我已经修改了我的解决scheme:

 getSignedUrl: function() { var options = config.gs , expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14) , bucket = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs }) , signed_url = null options.action = 'read' options.expires = expires// 2 weeks. options.resource= this.url Promise.promisifyAll(bucket); return bucket.getSignedUrlAsync(options).catch(function(err) { return url; // ignore errors and use the url instead }).then(function(signed_url) { return JSON.stringify( {url: signed_url, expires: expires} ); }); } 

我不清楚双重回报是如何运作的,但如果我保留回报桶

我得到的是这个输出:

{url:{_bitField:0,_fulfillmentHandler0:undefined,_rejectionHandler0:undefined,_promise0:undefined,_receiver0:undefined,_settledValue:undefined,_boundTo:undefined}}

,如果删除它,并保持

 return JSON.stringify( {url: signed_url, expires: expires} ); 

我像以前一样未定义。 我错过了什么?

一些观点:

  • 在一个new Promise(function(res, rej){ … })parsing器callback中,实际上需要调用 resolve()reject() (asynchronous),而不是return任何东西。
  • resolve不会返回任何东西。 你似乎使用它就像一个“等待”操作,返回承诺的结果,但这是不可能的。 承诺仍然是asynchronous的。
  • 其实,你永远不需要打电话给new Promise 。 改用Promisification 。

你的代码应该看起来像

 var gcloud = require('gcloud'); Promise.promisifyAll(gcloud); // if that doesn't work, call it once on a // specific bucket instead function getSignedUrl(url) { var options = config.gs, expires = Math.round(Date.now() / 1000) + (60 * 60 * 24 * 14), bucket = project.storage.bucket({bucketName: config.gs.bucket, credentials: config.gs }); options.action = 'read'; options.expires = expires; // 2 weeks. options.resource = url; return bucket.getSignedUrlAsync(options).catch(function(err) { return url; // ignore errors and use the url instead }).then(function(signed_url) { console.log("This is now defined: " + signed_url); return JSON.stringify( {url: signed_url, expires: expires} ); }); }