@@ -238,18 +238,17 @@ export function processReply(
238238 // Upgrade to use FormData to allow us to stream this value.
239239 formData = new FormData ( ) ;
240240 }
241+ pendingParts ++ ;
241242 try {
242243 const resolvedModel = init ( payload ) ;
243244 // We always outline this as a separate part even though we could inline it
244245 // because it ensures a more deterministic encoding.
245- pendingParts ++ ;
246246 const lazyId = nextPartId ++ ;
247247 const partJSON = JSON . stringify ( resolvedModel , resolveToJSON ) ;
248248 // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
249249 const data : FormData = formData ;
250250 // eslint-disable-next-line react-internal/safe-string-coercion
251251 data . append ( formFieldPrefix + lazyId , partJSON ) ;
252- pendingParts -- ;
253252 return serializeByValueID ( lazyId ) ;
254253 } catch ( x ) {
255254 if (
@@ -261,35 +260,33 @@ export function processReply(
261260 pendingParts ++ ;
262261 const lazyId = nextPartId ++ ;
263262 const thenable : Thenable < any > = (x: any);
264- thenable.then(
265- partValue => {
266- try {
267- const partJSON = JSON . stringify ( partValue , resolveToJSON ) ;
268- // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
269- const data : FormData = formData ;
270- // eslint-disable-next-line react-internal/safe-string-coercion
271- data . append ( formFieldPrefix + lazyId , partJSON ) ;
272- pendingParts -- ;
273- if ( pendingParts === 0 ) {
274- resolve ( data ) ;
275- }
276- } catch ( reason ) {
277- reject ( reason ) ;
263+ const retry = function() {
264+ // While the first promise resolved, its value isn't necessarily what we'll
265+ // resolve into because we might suspend again.
266+ try {
267+ const partJSON = JSON . stringify ( value , resolveToJSON ) ;
268+ // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
269+ const data : FormData = formData ;
270+ // eslint-disable-next-line react-internal/safe-string-coercion
271+ data . append ( formFieldPrefix + lazyId , partJSON ) ;
272+ pendingParts -- ;
273+ if ( pendingParts === 0 ) {
274+ resolve ( data ) ;
278275 }
279- } ,
280- reason => {
281- // In the future we could consider serializing this as an error
282- // that throws on the server instead.
276+ } catch ( reason ) {
283277 reject ( reason ) ;
284- } ,
285- );
278+ }
279+ }
280+ thenable . then ( retry , retry ) ;
286281 return serializeByValueID ( lazyId ) ;
287282 } else {
288283 // In the future we could consider serializing this as an error
289284 // that throws on the server instead.
290285 reject ( x ) ;
291286 return null ;
292287 }
288+ } finally {
289+ pendingParts -- ;
293290 }
294291 }
295292 }
0 commit comments