From d335addf0c729f51c4472a994de884608d110803 Mon Sep 17 00:00:00 2001 From: Xuguang Mei Date: Mon, 28 Mar 2022 18:44:21 +0800 Subject: [PATCH] doc: fix question promise API example PR-URL: https://github.com/nodejs/node/pull/42465 Reviewed-By: Antoine du Hamel Reviewed-By: Mestery Reviewed-By: James M Snell Reviewed-By: Darshan Sen Reviewed-By: Luigi Pinca --- doc/api/readline.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/doc/api/readline.md b/doc/api/readline.md index 69cfb2a6aee305..a116106d4c5d9f 100644 --- a/doc/api/readline.md +++ b/doc/api/readline.md @@ -591,7 +591,7 @@ added: v17.0.0 prompt. * `options` {Object} * `signal` {AbortSignal} Optionally allows the `question()` to be canceled - using an `AbortController`. + using an `AbortSignal`. * Returns: {Promise} A promise that is fulfilled with the user's input in response to the `query`. @@ -612,20 +612,17 @@ const answer = await rl.question('What is your favorite food? '); console.log(`Oh, so your favorite food is ${answer}`); ``` -Using an `AbortController` to cancel a question. +Using an `AbortSignal` to cancel a question. ```mjs -const ac = new AbortController(); -const signal = ac.signal; - -const answer = await rl.question('What is your favorite food? ', { signal }); -console.log(`Oh, so your favorite food is ${answer}`); +const signal = AbortSignal.timeout(10_000); signal.addEventListener('abort', () => { console.log('The food question timed out'); }, { once: true }); -setTimeout(() => ac.abort(), 10000); +const answer = await rl.question('What is your favorite food? ', { signal }); +console.log(`Oh, so your favorite food is ${answer}`); ``` ### Class: `readlinePromises.Readline`