Created
December 19, 2019 17:11
-
-
Save o0/997cbc4a3c54f90404077823fe546a7c 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
let myPromise = null; | |
const getNewPromise = () => { | |
if (!myPromise) { | |
myPromise = new Promise((resolve, reject) => { | |
setTimeout(() => { resolve('done'); }, 5000); | |
}); | |
} | |
return myPromise; | |
}; | |
// Вот этот код поставит два независимых колбэка, они вызовутся | |
// не по цепочке, а одновременно | |
getNewPromise().then(console.log); | |
getNewPromise().then(console.log); | |
// Вот этот код сделает цепочку и тут console.log сработает только | |
// один раз потому что первый then не возвращает promise | |
getNewPromise() | |
.then(console.log) | |
.then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment