Skip to content

Commit 39f985e

Browse files
committed
[js] Change some io operations to use native promises.
No need to incur the cost of the promise manager where it's not needed.
1 parent b8a8732 commit 39f985e

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Added `promise.Promise#catch()` for API compatibility with native Promises.
44
`promise.Promise#thenCatch()` is not yet deprecated, but it simply
55
delegates to `catch`.
6+
* Changed some `io` operations to use native promises.
67

78
## v2.48.2
89

javascript/node/selenium-webdriver/io/index.js

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
'use strict';
19+
1820
var fs = require('fs'),
1921
path = require('path'),
2022
rimraf = require('rimraf'),
2123
tmp = require('tmp');
2224

23-
var promise = require('..').promise;
24-
2525

2626

2727
// PUBLIC API
@@ -32,11 +32,11 @@ var promise = require('..').promise;
3232
* Recursively removes a directory and all of its contents. This is equivalent
3333
* to {@code rm -rf} on a POSIX system.
3434
* @param {string} path Path to the directory to remove.
35-
* @return {!promise.Promise} A promise to be resolved when the operation has
35+
* @return {!Promise} A promise to be resolved when the operation has
3636
* completed.
3737
*/
3838
exports.rmDir = function(path) {
39-
return new promise.Promise(function(fulfill, reject) {
39+
return new Promise(function(fulfill, reject) {
4040
var numAttempts = 0;
4141
attemptRm();
4242
function attemptRm() {
@@ -61,23 +61,19 @@ exports.rmDir = function(path) {
6161
* Copies one file to another.
6262
* @param {string} src The source file.
6363
* @param {string} dst The destination file.
64-
* @return {!promise.Promise.<string>} A promise for the copied file's path.
64+
* @return {!Promise<string>} A promise for the copied file's path.
6565
*/
6666
exports.copy = function(src, dst) {
67-
var copied = promise.defer();
68-
69-
var rs = fs.createReadStream(src);
70-
rs.on('error', copied.reject);
71-
rs.on('end', function() {
72-
copied.fulfill(dst);
73-
});
67+
return new Promise(function(fulfill, reject) {
68+
var rs = fs.createReadStream(src);
69+
rs.on('error', reject);
70+
rs.on('end', () => fulfill(dst));
7471

75-
var ws = fs.createWriteStream(dst);
76-
ws.on('error', copied.reject);
72+
var ws = fs.createWriteStream(dst);
73+
ws.on('error', reject);
7774

78-
rs.pipe(ws);
79-
80-
return copied.promise;
75+
rs.pipe(ws);
76+
});
8177
};
8278

8379

@@ -88,7 +84,7 @@ exports.copy = function(src, dst) {
8884
* @param {(RegEx|function(string): boolean)=} opt_exclude An exclusion filter
8985
* as either a regex or predicate function. All files matching this filter
9086
* will not be copied.
91-
* @return {!promise.Promise.<string>} A promise for the destination
87+
* @return {!Promise<string>} A promise for the destination
9288
* directory's path once all files have been copied.
9389
*/
9490
exports.copyDir = function(src, dst, opt_exclude) {
@@ -128,32 +124,30 @@ exports.copyDir = function(src, dst, opt_exclude) {
128124
}
129125
});
130126

131-
return promise.all(results).then(function() {
132-
return dst;
133-
});
127+
return Promise.all(results).then(() => dst);
134128
};
135129

136130

137131
/**
138132
* Tests if a file path exists.
139133
* @param {string} path The path to test.
140-
* @return {!promise.Promise.<boolean>} A promise for whether the file exists.
134+
* @return {!Promise.<boolean>} A promise for whether the file exists.
141135
*/
142136
exports.exists = function(path) {
143-
var result = promise.defer();
144-
fs.exists(path, result.fulfill);
145-
return result.promise;
137+
return new Promise(function(fulfill) {
138+
fs.exists(path, fulfill);
139+
});
146140
};
147141

148142

149143
/**
150144
* Deletes a name from the filesystem and possibly the file it refers to. Has
151145
* no effect if the file does not exist.
152146
* @param {string} path The path to remove.
153-
* @return {!promise.Promise} A promise for when the file has been removed.
147+
* @return {!Promise} A promise for when the file has been removed.
154148
*/
155149
exports.unlink = function(path) {
156-
return new promise.Promise(function(fulfill, reject) {
150+
return new Promise(function(fulfill, reject) {
157151
fs.exists(path, function(exists) {
158152
if (exists) {
159153
fs.unlink(path, function(err) {
@@ -168,27 +162,37 @@ exports.unlink = function(path) {
168162

169163

170164
/**
171-
* @return {!promise.Promise.<string>} A promise for the path to a temporary
172-
* directory.
165+
* @return {!Promise<string>} A promise for the path to a temporary directory.
173166
* @see https://www.npmjs.org/package/tmp
174167
*/
175168
exports.tmpDir = function() {
176-
return promise.checkedNodeCall(tmp.dir);
169+
return new Promise(function(fulfill, reject) {
170+
tmp.dir(function(error, value) {
171+
error ? reject(error) : fulfill(value);
172+
});
173+
});
177174
};
178175

179176

180177
/**
181178
* @param {{postfix: string}=} opt_options Temporary file options.
182-
* @return {!promise.Promise.<string>} A promise for the path to a temporary
183-
* file.
179+
* @return {!Promise<string>} A promise for the path to a temporary file.
184180
* @see https://www.npmjs.org/package/tmp
185181
*/
186182
exports.tmpFile = function(opt_options) {
187-
// |tmp.file| checks arguments length to detect options rather than doing a
188-
// truthy check, so we must only pass options if there are some to pass.
189-
return opt_options ?
190-
promise.checkedNodeCall(tmp.file, opt_options) :
191-
promise.checkedNodeCall(tmp.file);
183+
return new Promise(function(fulfill, reject) {
184+
let callback = function(err, value) {
185+
err ? reject(err) : fulfill(value);
186+
};
187+
188+
// |tmp.file| checks arguments length to detect options rather than doing a
189+
// truthy check, so we must only pass options if there are some to pass.
190+
if (opt_options) {
191+
tmp.file(opt_options, callback);
192+
} else {
193+
tmp.file(callback);
194+
}
195+
});
192196
};
193197

194198

0 commit comments

Comments
 (0)