# FileLoader: getting data ouf of the onLoad function?

**URL:** <https://discourse.threejs.org/t/fileloader-getting-data-ouf-of-the-onload-function/36402>\
**Category:** Questions\
**Tags:** loaders\
**Created:** [March 26, 2022, 5:00pm UTC](https://discourse.threejs.org/t/fileloader-getting-data-ouf-of-the-onload-function/36402 "2022-03-26T17:00:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Primergy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/primergy/32/25223_2.png) [@Primergy](https://discourse.threejs.org/u/Primergy)\
**Post date:** [March 26, 2022, 5:00pm UTC](https://discourse.threejs.org/t/fileloader-getting-data-ouf-of-the-onload-function/36402/1 "2022-03-26T17:00:03Z")

</div>

Hey,  
i’m currently trying to use the FileLoader to load an external XML file, but i have a hard time to grasp how to get usable data from it.

As in the documentation i can show the data of the file in the console, but i cannot return it. I tried to assign it to a variable outside of the onLoad-function but it didn’t worked. May it an idea to make it async?

My code (the file is delivered by another module and the function should return the data):

```javascript
// load xml file
import { FileLoader } from '../../../../vendor/three/build/three.module.js';
import { Cache } from '../../../vendor/three/build/three.module.js';

Cache.enabled = true;

function loadFile ( file ) {
	const loader = new FileLoader();

	const something = loader.load(

		file,

		//onLoad callback
		function ( data ) {
			console.log(data);
			},
	)

	return something ;
}

export { loadFile };

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Benny](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/benny/32/25234_2.png) [@Benny](https://discourse.threejs.org/u/Benny)\
**Post date:** [March 26, 2022, 7:04pm UTC](https://discourse.threejs.org/t/fileloader-getting-data-ouf-of-the-onload-function/36402/2 "2022-03-26T19:04:54Z")

</div>

`loader.load` is an asynchronous function. That is why you need to provide an `onLoad` callback. If you want to work with asynchronous functions in a synchronous way (syntax wise, they still async of course) you can use Promises with `async/await`.

Have look at this example:

```javascript
import * as THREE from 'three';

function loadFile(filePath) {
    return new Promise((resolve) => {
        const loader = new THREE.FileLoader();

        loader.load(filePath, (data) => {
            resolve(data);
        });
    });
}

async function test() {
    const data = await loadFile('example.txt');
    console.log(data);
}

void test();

```
