Skip to content

Commit 53df668

Browse files
committed
Add usage cases
1 parent 302fc12 commit 53df668

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

JavaScript/8-promisify.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,25 @@ const main = async () => {
2020
const fileName = '8-promisify.js';
2121
const data = await read(fileName, 'utf8');
2222
console.log(`File "${fileName}" size: ${data.length}`);
23+
24+
// Call from try/catch block
2325
try {
2426
const data = await read('unknown.file', 'utf8');
2527
console.log(`File size: ${data.length}`);
2628
} catch (error) {
2729
console.error(error.message);
2830
}
31+
32+
// Set default value in promise.catch block
33+
const content = await read('unknown.file').catch((error) => {
34+
console.error(error.message);
35+
return null;
36+
});
37+
// Warning: note that you need additional if-statement
38+
if (content) {
39+
console.log({ content });
40+
console.log(`File size: ${content.size}`);
41+
}
2942
};
3043

3144
main();

0 commit comments

Comments
 (0)