From 51fc02f2bf0dac3489a46e38bf9d6da0a52a36ed Mon Sep 17 00:00:00 2001 From: hopperelec Date: Tue, 6 Aug 2024 03:54:31 +0100 Subject: [PATCH] Fixed `catch` in abort examples --- examples/abort/any-request.ts | 18 +++++++++--------- examples/abort/specific-request.ts | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/abort/any-request.ts b/examples/abort/any-request.ts index 84ae5c4..a553ffb 100644 --- a/examples/abort/any-request.ts +++ b/examples/abort/any-request.ts @@ -6,8 +6,7 @@ setTimeout(() => { ollama.abort() }, 1000) // 1000 milliseconds = 1 second -try { - ollama.generate({ +ollama.generate({ model: 'llama3.1', prompt: 'Write a long story', stream: true, @@ -17,11 +16,12 @@ try { process.stdout.write(chunk.response) } } + ).catch( + (error) => { + if (error.name === 'AbortError') { + console.log('The request has been aborted') + } else { + console.error('An error occurred:', error) + } + } ) -} catch (error) { - if (error.name === 'AbortError') { - console.log('The request has been aborted') - } else { - console.error('An error occurred:', error) - } -} diff --git a/examples/abort/specific-request.ts b/examples/abort/specific-request.ts index 7b57987..778c514 100644 --- a/examples/abort/specific-request.ts +++ b/examples/abort/specific-request.ts @@ -9,8 +9,7 @@ setTimeout(() => { stream.abort() }, 1000) // 1000 milliseconds = 1 second -try { - ollama.generate({ +ollama.generate({ model: 'llama3.1', prompt: 'Write a long story', stream: true, @@ -21,11 +20,12 @@ try { process.stdout.write(chunk.response) } } + ).catch( + (error) => { + if (error.name === 'AbortError') { + console.log('The request has been aborted') + } else { + console.error('An error occurred:', error) + } + } ) -} catch (error) { - if (error.name === 'AbortError') { - console.log('The request has been aborted') - } else { - console.error('An error occurred:', error) - } -}