Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/bigquery/src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,27 @@ Job.prototype.listenForEvents_ = function() {
});
};

/**
* Convenience method that wraps the `complete` and `error` events in a
* Promise.
*
* @return {promise}
*
* @example
* job.promise().then(function(metadata) {
* // The job is complete.
* }, function(err) {
* // An error occurred during the job.
* });
*/
Job.prototype.promise = function() {

This comment was marked as spam.

This comment was marked as spam.

var self = this;

return new common.util.Promise(function(resolve, reject) {
self.on('error', reject).on('complete', resolve);
});
};

/**
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
* the API on an interval.
Expand Down
13 changes: 13 additions & 0 deletions packages/common/src/grpc-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ GrpcOperation.prototype.listenForEvents_ = function() {
});
};

/**
* Wraps the `complete` and `error` events in a Promise.
*
* @return {promise}
*/
GrpcOperation.prototype.promise = function() {

This comment was marked as spam.

This comment was marked as spam.

var self = this;

return new util.Promise(function(resolve, reject) {
self.on('error', reject).on('complete', resolve);
});
};

/**
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
* the API on an interval.
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Service(config, options) {
this.projectIdRequired = config.projectIdRequired !== false;

if (options.promise) {
util.setPromiseOverride(options.promise);
util.Promise = options.promise;
}
}

Expand Down
25 changes: 8 additions & 17 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ var errorMessage = format([
path: '/docs/guides/authentication'
});

var PromiseOverride;

var missingProjectIdError = new Error(errorMessage);

util.missingProjectIdError = missingProjectIdError;
Expand Down Expand Up @@ -634,6 +632,13 @@ function getUserAgentFromPackageJson(packageJson) {

util.getUserAgentFromPackageJson = getUserAgentFromPackageJson;

/**
* Rather than making getters and setters for getting the Promise override
* let's just store it on the util object and reference that.
* By default we'll use the native Promise object.
*/
util.Promise = Promise;

/**
* Wraps a callback style function to conditionally return a promise.
*
Expand All @@ -656,9 +661,7 @@ function promisify(originalMethod) {
return originalMethod.apply(context, args);
}

var PromiseCtor = PromiseOverride || Promise;

return new PromiseCtor(function(resolve, reject) {
return new util.Promise(function(resolve, reject) {
args.push(function() {
var callbackArgs = slice.call(arguments);
var err = callbackArgs.shift();
Expand Down Expand Up @@ -708,15 +711,3 @@ function promisifyAll(Class, options) {
}

util.promisifyAll = promisifyAll;

/**
* Allows user to override the Promise constructor without the need to touch
* globals. Override should be ES6 Promise compliant.
*
* @param {promise} override
*/
function setPromiseOverride(override) {
PromiseOverride = override;
}

module.exports.setPromiseOverride = setPromiseOverride;
21 changes: 21 additions & 0 deletions packages/compute/src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,27 @@ Operation.prototype.listenForEvents_ = function() {
});
};

/**
* Convenience method that wraps the `complete` and `error` events in a
* Promise.
*
* @return {promise}
*
* @example
* operation.promise().then(function(metadata) {
* // The operation is complete.
* }, function(err) {
* // An error occurred during the operation.
* });
*/
Operation.prototype.promise = function() {
var self = this;

return new common.util.Promise(function(resolve, reject) {
self.on('error', reject).on('complete', resolve);
});
};

/**
* Poll `getMetadata` to check the operation's status. This runs a loop to ping
* the API on an interval.
Expand Down