Skip to content

Commit

Permalink
Core & multiple modules: refactor usage of Promise to avoid deferrals…
Browse files Browse the repository at this point in the history
… when possible (6.29.x) (prebid#8672)

* Core & multiple modules: refactor usage of Promise to avoid deferrals when possible

* Unhandled rejection handling

* Improve tests
  • Loading branch information
dgirardi authored and xiekevin committed Jan 19, 2023
1 parent 266a119 commit 4f725d4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,12 @@ function idSystemInitializer({delay = GreedyPromise.timeout} = {}) {
cancel.reject(INIT_CANCELED);
}
cancel = defer();
<<<<<<< HEAD
return GreedyPromise.race([promise, cancel.promise])
.finally(initMetrics.startTiming('userId.total'))
=======
return GreedyPromise.race([promise, cancel.promise]);
>>>>>>> a8c7a440e (Core & multiple modules: refactor usage of Promise to avoid deferrals when possible (6.29.x) (#8672))
}
// grab a reference to global vars so that the promise chains remain isolated;
Expand All @@ -536,7 +540,11 @@ function idSystemInitializer({delay = GreedyPromise.timeout} = {}) {
let done = cancelAndTry(
GreedyPromise.all([hooksReady, startInit.promise])
<<<<<<< HEAD
.then(timeGdpr)
=======
.then(() => gdprDataHandler.promise)
>>>>>>> a8c7a440e (Core & multiple modules: refactor usage of Promise to avoid deferrals when possible (6.29.x) (#8672))
.then(checkRefs((consentData) => {
initSubmodules(initModules, allModules, consentData);
}))
Expand Down Expand Up @@ -616,9 +624,15 @@ function getPPID(eids = getUserIdsAsEids() || []) {
* @param {Object} reqBidsConfigObj required; This is the same param that's used in pbjs.requestBids.
* @param {function} fn required; The next function in the chain, used by hook.js
*/
<<<<<<< HEAD
export const requestBidsHook = timedAuctionHook('userId', function requestBidsHook(fn, reqBidsConfigObj, {delay = GreedyPromise.timeout, getIds = getUserIdsAsync} = {}) {
GreedyPromise.race([
getIds().catch(() => null),
=======
export function requestBidsHook(fn, reqBidsConfigObj, {delay = GreedyPromise.timeout} = {}) {
GreedyPromise.race([
getUserIdsAsync(),
>>>>>>> a8c7a440e (Core & multiple modules: refactor usage of Promise to avoid deferrals when possible (6.29.x) (#8672))
delay(auctionDelay)
]).then(() => {
// pass available user id data to bid adapters
Expand Down Expand Up @@ -752,6 +766,7 @@ function refreshUserIds({submoduleNames} = {}, callback) {
function getUserIdsAsync() {
return initIdSystem().then(
() => getUserIds(),
<<<<<<< HEAD
(e) => {
if (e === INIT_CANCELED) {
// there's a pending refresh - because GreedyPromise runs this synchronously, we are now in the middle
Expand All @@ -763,6 +778,15 @@ function getUserIdsAsync() {
return GreedyPromise.reject(e)
}
}
=======
(e) =>
e === INIT_CANCELED
// there's a pending refresh - because GreedyPromise runs this synchronously, we are now in the middle
// of canceling the previous init, before the refresh logic has had a chance to run.
// Use a "normal" Promise to clear the stack and let it complete (or this will just recurse infinitely)
? Promise.resolve().then(getUserIdsAsync)
: GreedyPromise.reject(e)
>>>>>>> a8c7a440e (Core & multiple modules: refactor usage of Promise to avoid deferrals when possible (6.29.x) (#8672))
);
}

Expand Down

0 comments on commit 4f725d4

Please sign in to comment.