Forked from lmammino/promise-with-resolvers-polyfill.js
Created
July 12, 2024 08:55
-
-
Save eric-gitta-moore/8866989fb7cd5a2c94d94806e92a840d to your computer and use it in GitHub Desktop.
Promise.withResolvers() polyfill
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
if (typeof Promise.withResolvers === 'undefined') { | |
Promise.withResolvers = function () { | |
let resolve, reject | |
const promise = new Promise((res, rej) => { | |
resolve = res | |
reject = rej | |
}) | |
return { promise, resolve, reject } | |
} | |
} | |
// Usage: | |
// const { promise, resolve, reject } = Promise.withResolvers() | |
// console.log(promise, resolve, reject) // Promise { <pending> } [Function (anonymous)] [Function (anonymous)] | |
// ... Do something async and then call resolve or reject! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment