Skip to content

Commit 8525aa1

Browse files
authored
Promisify askPermission (#61)
1 parent 41d6b38 commit 8525aa1

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

lib/index.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,11 @@ class Insight {
141141
this._save();
142142
}
143143

144-
askPermission(msg, cb) {
144+
askPermission(msg) {
145145
const defaultMsg = `May ${chalk.cyan(this.packageName)} anonymously report usage statistics to improve the tool over time?`;
146146

147-
cb = cb || (() => {});
148-
149147
if (!process.stdout.isTTY || process.argv.indexOf('--no-insight') !== -1 || process.env.CI) {
150-
setImmediate(cb, null, false);
151-
return;
148+
return Promise.resolve();
152149
}
153150

154151
const prompt = inquirer.prompt({
@@ -158,23 +155,29 @@ class Insight {
158155
default: true
159156
});
160157

161-
// Add a 30 sec timeout before giving up on getting an answer
162-
const permissionTimeout = setTimeout(() => {
163-
// Stop listening for stdin
164-
prompt.ui.close();
165-
166-
// Automatically opt out
167-
this.optOut = true;
168-
cb(null, false);
169-
}, this._permissionTimeout * 1000);
158+
// Set a 30 sec timeout before giving up on getting an answer
159+
let permissionTimeout;
160+
const timeoutPromise = new Promise(resolve => {
161+
permissionTimeout = setTimeout(() => {
162+
// Stop listening for stdin
163+
prompt.ui.close();
164+
165+
// Automatically opt out
166+
this.optOut = true;
167+
resolve(false);
168+
}, this._permissionTimeout * 1000);
169+
});
170170

171-
prompt.then(result => {
171+
const promise = prompt.then(result => {
172172
// Clear the permission timeout upon getting an answer
173173
clearTimeout(permissionTimeout);
174174

175175
this.optOut = !result.optIn;
176-
cb(null, result.optIn);
176+
return result.optIn;
177177
});
178+
179+
// Return the result of the prompt if it finishes first otherwise default to the timeout's value.
180+
return Promise.race([promise, timeoutPromise]);
178181
}
179182
}
180183

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ Type: `integer`
186186

187187
Event value: A numeric value associated with the event (e.g. 42).
188188

189-
#### .askPermission([message, callback])
189+
#### .askPermission([message])
190190

191191
Asks the user permission to opt-in to tracking and sets the `optOut` property in `config`. You can also choose to set `optOut` property in `config` manually.
192192

193193
![askPermission screenshot](screenshot-askpermission.png)
194194

195-
Optionally supply your own `message` and `callback`. If `message` is `null`, default message will be used. The callback will be called with the arguments `error` and `optIn` when the prompt is done and is useful for when you want to continue the execution while the prompt is running.
195+
Optionally supply your own `message`. If `message` is `null`, default message will be used. This also resolves with the new value of `optIn` when the prompt is done and is useful for when you want to continue the execution while the prompt is running.
196196

197197
#### .optOut
198198

test/fixtures/sub-process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ if (process.env.permissionTimeout) {
1111
insight._permissionTimeout = process.env.permissionTimeout;
1212
}
1313

14-
insight.askPermission('', () => {
14+
insight.askPermission('').then(() => {
1515
process.exit(145);
1616
});

0 commit comments

Comments
 (0)