Repository: facebook/react
File: packages/react-server/src/ReactFlightServer.js (JavaScript)

Code before:
        if (ioNode === undefined) {
          // Undefined is used as a signal that we found a suitable aborted node and we don't have to find
          // further aborted nodes.
          return undefined;
        } else if (ioNode !== null) {
          const startTime: number = node.start;
          const endTime: number = node.end;
          if (endTime <= request.timeOrigin) {
            // This was already resolved when we started this render. It must have been either something
            // that's part of a start up sequence or externally cached data. We exclude that information.
            return null;
          } else if (startTime < cutOff) {
            // We started awaiting this node before we started rendering this sequence.
            // This means that this particular await was never part of the current sequence.
            // If we have another await higher up in the chain it might have a more actionable stack
            // from the perspective of this component. If we end up here from the "previous" path,
            // then this gets I/O ignored, which is what we want because it means it was likely
            // just part of a previous component's rendering.
            match = ioNode;
          } else {
            let isAwaitInUserspace = false;
            const fullStack = node.stack;
            if (fullStack.length > 0) {
              // Check if the very first stack frame that awaited this Promise was in user space.
              // TODO: This doesn't take into account wrapper functions such as our fake .then()
              // in FlightClient which will always be considered third party awaits if you call
              // .then directly.
              const filterStackFrame = request.filterStackFrame;
              const callsite = fullStack[0];
              const functionName = callsite[0];
              const url = devirtualizeURL(callsite[1]);
              isAwaitInUserspace = filterStackFrame(url, functionName);
            }
            if (!isAwaitInUserspace) {
              // If this await was fully filtered out, then it was inside third party code
              // such as in an external library. We return the I/O node and try another await.
              match = ioNode;
            } else if (
              request.status === ABORTING &&
              startTime > request.abortTime
            ) {
              // This was awaited after aborting so we skip it.
            } else {
              // We found a user space await.

              // Outline the IO node.
              // The ioNode is where the I/O was initiated, but after that it could have been
              // processed through various awaits in the internals of the third party code.
              // Therefore we don't use the inner most Promise as the conceptual value but the
              // Promise that was ultimately awaited by the user space await.
              serializeIONode(request, ioNode, awaited.promise);

Review comment (suggestion):
If you call raw `.then()` then that gets a stack frame that's not in user space which we need to ignore. The first frame after user space should be considered for the heuristic.

Code after:
        if (ioNode === undefined) {
          // Undefined is used as a signal that we found a suitable aborted node and we don't have to find
          // further aborted nodes.
          return undefined;
        } else if (ioNode !== null) {
          const startTime: number = node.start;
          const endTime: number = node.end;
          if (endTime <= request.timeOrigin) {
            // This was already resolved when we started this render. It must have been either something
            // that's part of a start up sequence or externally cached data. We exclude that information.
            return null;
          } else if (startTime < cutOff) {
            // We started awaiting this node before we started rendering this sequence.
            // This means that this particular await was never part of the current sequence.
            // If we have another await higher up in the chain it might have a more actionable stack
            // from the perspective of this component. If we end up here from the "previous" path,
            // then this gets I/O ignored, which is what we want because it means it was likely
            // just part of a previous component's rendering.
            match = ioNode;
          } else {
            let isAwaitInUserspace = false;
            const fullStack = node.stack;
            let firstFrame = 0;
            while (
              fullStack.length > firstFrame &&
              fullStack[firstFrame][0] === 'Promise.then'
            ) {
              // Skip Promise.then frame itself.
              firstFrame++;
            }
            if (fullStack.length > firstFrame) {
              // Check if the very first stack frame that awaited this Promise was in user space.
              // TODO: This doesn't take into account wrapper functions such as our fake .then()
              // in FlightClient which will always be considered third party awaits if you call
              // .then directly.
              const filterStackFrame = request.filterStackFrame;
              const callsite = fullStack[firstFrame];
              const functionName = callsite[0];
              const url = devirtualizeURL(callsite[1]);
              isAwaitInUserspace = filterStackFrame(url, functionName);
            }
            if (!isAwaitInUserspace) {
              // If this await was fully filtered out, then it was inside third party code
              // such as in an external library. We return the I/O node and try another await.
              match = ioNode;
            } else if (
              request.status === ABORTING &&
              startTime > request.abortTime
            ) {
              // This was awaited after aborting so we skip it.
            } else {