@@ -219,7 +219,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
219219 */
220220 constructor ( config : optimizely . Config ) {
221221 this . initialConfig = config ;
222- this . userPromiseResolver = ( ) => { } ;
222+ this . userPromiseResolver = ( ) => { } ;
223223
224224 const configWithClientInfo = {
225225 ...config ,
@@ -244,9 +244,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
244244 } ) ;
245245
246246 this . dataReadyPromise = Promise . all ( [ this . userPromise , this . _client ! . onReady ( ) ] ) . then ( res => {
247- if ( ! res [ 0 ] . success ) {
248- return res [ 0 ] ;
249- }
250247
251248 // Client and user can become ready synchronously and/or asynchronously. This flag specifically indicates that they became ready asynchronously.
252249 this . isReadyPromiseFulfilled = true ;
@@ -309,8 +306,18 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
309306 } , timeout ) as any ;
310307 } ) ;
311308
312- return Promise . race ( [ this . dataReadyPromise , timeoutPromise ] ) . then ( res => {
309+ return Promise . race ( [ this . dataReadyPromise , timeoutPromise ] ) . then ( async res => {
313310 clearTimeout ( timeoutId ) ;
311+ if ( res . success ) {
312+ const isSegmentsFetched = await this . fetchQualifiedSegments ( ) ;
313+ if ( ! isSegmentsFetched ) {
314+ return {
315+ success : false ,
316+ reason : 'USER_NOT_READY' ,
317+ message : 'Failed to fetch qualified segments' ,
318+ }
319+ }
320+ }
314321 return res ;
315322 } ) ;
316323 }
@@ -357,7 +364,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
357364 }
358365
359366 public async setUser ( userInfo : UserInfo ) : Promise < void > {
360- this . isUserPromiseResolved = false ;
361367 this . isUserReady = true ;
362368
363369 //reset user info
@@ -377,16 +383,13 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
377383 if ( userInfo . attributes ) {
378384 this . user . attributes = userInfo . attributes ;
379385 }
380- const isSegmentsFetched = await this . fetchQualifiedSegments ( ) ;
381386
382- const segmentsResult : ResolveResult = { success : isSegmentsFetched } ;
383- if ( ! isSegmentsFetched ) {
384- segmentsResult . reason = 'USER_NOT_READY' ;
385- segmentsResult . message = 'Failed to fetch qualified segments' ;
387+ if ( this . getIsReadyPromiseFulfilled ( ) ) {
388+ await this . fetchQualifiedSegments ( ) ;
386389 }
387390
388391 if ( ! this . isUserPromiseResolved ) {
389- this . userPromiseResolver ( segmentsResult ) ;
392+ this . userPromiseResolver ( { success : true } ) ;
390393 this . isUserPromiseResolved = true ;
391394 }
392395
0 commit comments