src/vs/workbench/services/textfile/common/textFileEditorModelManager.ts/TextFileEditorModelManager/resolve
class TextFileEditorModelManager:	async resolve(resource: URI, options?: ITextFileEditorModelResolveOrCreateOptions): Promise<TextFileEditorModel> {

		// Await a pending model resolve first before proceeding
		// to ensure that we never resolve a model more than once
		// in parallel
		const pendingResolve = this.joinPendingResolve(resource);
		if (pendingResolve) {
			await pendingResolve;
		}

		let modelPromise: Promise<void>;
		let model = this.get(resource);
		let didCreateModel = false;

		// Model exists
		if (model) {

			// Always reload if contents are provided
			if (options?.contents) {
				modelPromise = model.resolve(options);
			}

			// Reload async or sync based on options
			else if (options?.reload) {

				// async reload: trigger a reload but return immediately
				if (options.reload.async) {
					modelPromise = Promise.resolve();
					model.resolve(options);
				}

				// sync reload: do not return until model reloaded
				else {
					modelPromise = model.resolve(options);
				}
			}

			// Do not reload
			else {
				modelPromise = Promise.resolve();
			}
		}

		// Model does not exist
		else {
			didCreateModel = true;

			const newModel = model = this.instantiationService.createInstance(TextFileEditorModel, resource, options ? options.encoding : undefined, options ? options.mode : undefined);
			modelPromise = model.resolve(options);

			this.registerModel(newModel);
		}

		// Store pending resolves to avoid race conditions
		this.mapResourceToPendingModelResolvers.set(resource, modelPromise);

		// Make known to manager (if not already known)
		this.add(resource, model);

		// Emit some events if we created the model
		if (didCreateModel) {
			this._onDidCreate.fire(model);

			// If the model is dirty right from the beginning,
			// make sure to emit this as an event
			if (model.isDirty()) {
				this._onDidChangeDirty.fire(model);
			}
		}

		try {
			await modelPromise;

			// Remove from pending resolves
			this.mapResourceToPendingModelResolvers.delete(resource);

			// Apply mode if provided
			if (options?.mode) {
				model.setMode(options.mode);
			}

			// Model can be dirty if a backup was restored, so we make sure to
			// have this event delivered if we created the model here
			if (didCreateModel && model.isDirty()) {
				this._onDidChangeDirty.fire(model);
			}

			return model;
		} catch (error) {

			// Free resources of this invalid model
			if (model) {
				model.dispose();
			}

			// Remove from pending resolves
			this.mapResourceToPendingModelResolvers.delete(resource);

			throw error;
		}
	}