Created
March 14, 2025 14:08
-
-
Save runeh/60aa0df7a729fdda8541aea3f12c77af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DataLoader from 'dataloader'; | |
async function main() { | |
const loader = new DataLoader( | |
// eslint-disable-next-line require-await | |
async (keys: readonly string[]) => { | |
const results = keys.map(key => { | |
if (key === 'return error') { | |
return new Error('"returned error"'); | |
} else if (key === 'throw error') { | |
throw new Error('"thrown error"'); | |
} else { | |
return `Ok: ${key}`; | |
} | |
}); | |
return results; | |
}, | |
{ cache: false }, | |
); | |
console.log('loadOneOkValue'); | |
const loadOneOkValue = await loader.load('1'); | |
console.log(loadOneOkValue); | |
console.log('\n-------------\n'); | |
console.log('loadManyOkValue'); | |
const loadManyOkValue = await loader.loadMany(['1', '2', '3']); | |
console.log(loadManyOkValue); | |
console.log('\n-------------\n'); | |
console.log('loadOkAndError'); | |
const loadOkAndError = await loader.loadMany(['1', 'return error', '2']); | |
console.log(loadOkAndError); | |
console.log('\n-------------\n'); | |
try { | |
console.log('loadOneError'); | |
const loadOneError = await loader.load('return error'); | |
console.log(loadOneError); | |
} catch (error) { | |
console.log('it threw', error); | |
} | |
console.log('\n-------------\n'); | |
try { | |
console.log('loadOneErrorWhereLoaderThrows'); | |
const loadOneErrorWhereLoaderThrows = await loader.load('throw error'); | |
console.log(loadOneErrorWhereLoaderThrows); | |
} catch (error) { | |
console.log('it threw', error); | |
} | |
console.log('\n-------------\n'); | |
try { | |
console.log('loadManyWhereLoaderThrows'); | |
const loadManyWhereLoaderThrows = await loader.loadMany([ | |
'1', | |
'throw error', | |
'2', | |
]); | |
console.log(loadManyWhereLoaderThrows); | |
} catch (error) { | |
console.log('it threw', error); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment