Created
November 30, 2024 15:41
-
-
Save romiras/9f35283d0da70ed5f0504a702162ef5d to your computer and use it in GitHub Desktop.
TypeScript code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface StatusRequestBody { | |
query: { | |
match: { status_id: string }; | |
}; | |
} | |
await new Promise(resolve => setTimeout(resolve, 500)); | |
return new Promise<void>((resolve, reject) => { | |
this.client.quit(); | |
resolve(); | |
}); | |
const asyncFunc: () => Promise<void> = async () => { | |
await new Promise(resolve => resolve()); | |
}; | |
// https://developer.mozilla.org/ru/docs/Learn/JavaScript/Asynchronous/Async_await | |
async Connect(): Promise<void> { | |
await this.client.connect(); | |
console.log('==> REDIS!!'); | |
await this.kk(); | |
} | |
async Close(): Promise<void> { | |
return this.client.quit(); | |
} | |
async kk(): Promise<void> { | |
const value = await this.client.get(`foo`); | |
console.log('value = ', value); | |
} | |
// rejectable promises: | |
function makeRejectable<Value>(src: Promise<Value>): [Promise<Value>, (err?: unknown) => void] { | |
let reject: ((err?: unknown) => void) = () => {}; | |
const promise = new Promise<Value>((res, rej) => { | |
reject = rej; | |
src.then(res, rej); | |
}); | |
return [promise, reject]; | |
} | |
const [promise, reject] = makeRejectable(Promise.all([1, 'q'])); | |
promise.then(console.log, console.error); | |
reject('reject'); | |
//setTimeout(reject, 1000, 'reject'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment