Реализация двух конструкторных функций Promise.resolve
и Promise.reject
Алгоритмы имеют зависимость от NewPromiseCapability
Last active
February 3, 2024 11:50
-
-
Save dSalieri/00b2213ee2e95b95ad1da3495e37646e to your computer and use it in GitHub Desktop.
Promise.resolve/Promise.reject
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
/// Так выглядит реализация Promise.reject | |
Promise._reject = function(r){ | |
const C = this; | |
const promiseCapability = NewPromiseCapability(C); | |
promiseCapability["[[Reject]]"](r); | |
return promiseCapability["[[Promise]]"]; | |
} |
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
/// Так выглядит реализация Promise.resolve | |
Promise._resolve = function(x){ | |
const C = this; | |
if (typeof C !== "object" && typeof C !== "function" || C === null) throw TypeError("this is not object"); | |
const resolvePromise = (C, x) => { | |
if(x instanceof Promise){ | |
const xConstructor = x.constructor; | |
if(C === xConstructor) return x; | |
} | |
const promiseCapability = NewPromiseCapability(C); | |
promiseCapability["[[Resolve]]"](x); | |
return promiseCapability["[[Promise]]"]; | |
} | |
return resolvePromise(C, x); | |
} |
@Vebnik эта реализация основана на шагах алгоритмов спецификации.
Как узнали об этом gist? Я нигде его не публиковал :)
Знакомый кидал мне другой gist(где он его нашёл я не знаю), я решил остальные посмотреть у вас )
Для меня это находка, так как пишу на js довольно много, но некоторые конструкции как чёрная коробка.
То есть я не знаю как они работают и реализуются, просто принимаю факт их работы и всё )
@Vebnik интересно... значит ваш знакомый был на learn.javascript.ru, т.к я там оставлял развернутые комментарии с ссылками на gist'ы.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Где-то на подсознательном уровне приходили мысли насчёт реализации функций
Promise.resolve
иPromise.reject
.Спасибо за примеры.