Created
February 7, 2025 14:42
-
-
Save Kcko/c0796d958b5f1d9662f84429564a43ab to your computer and use it in GitHub Desktop.
This file contains 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
function foo() | |
{ | |
const {promise, resolve, reject} = Promise.withResolvers() | |
setTimeout(() => { | |
resolve('DONE') | |
}, 2000) | |
return promise | |
} | |
function foo2() | |
{ | |
const {promise, resolve, reject} = withResolvers() | |
setTimeout(() => { | |
resolve('DONE 2') | |
}, 2500) | |
return promise | |
} | |
// polyfill | |
function withResolvers() { | |
let resolve, reject; | |
const promise = new Promise((res, rej) => { | |
resolve = res; | |
reject = rej; | |
}); | |
return { promise, resolve, reject }; | |
} | |
// Použití: | |
const { promise: aliasOnlyForTesting, resolve } = withResolvers(); | |
(async() => { | |
const data = await foo() | |
console.log(data) | |
const data2 = await foo2() | |
console.log(data2) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment