Skip to content

Commit b2035ae

Browse files
committed
Use async functions instead of IIFE
1 parent f6656a9 commit b2035ae

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

JavaScript/d-resolvers.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const sumAsync = (a, b, callback) => {
1212

1313
// Old approach
1414

15-
(async () => {
15+
const old = async () => {
1616
let resolve, reject;
1717
const promise = new Promise((resolved, rejected) => {
1818
resolve = resolved;
@@ -22,27 +22,31 @@ const sumAsync = (a, b, callback) => {
2222
sumAsync(2, 3, resolve);
2323
const result = await promise;
2424
console.log({ result });
25-
})();
25+
};
26+
27+
old();
2628

2729
// Alternative approach
2830

29-
(async () => {
31+
const alternative = async () => {
3032
const promise = new Promise((resolve, reject) => {
3133
sumAsync(4, 5, resolve);
3234
setTimeout(reject, 1000, new Error('Timed out'));
3335
});
3436
const result = await promise;
3537
console.log({ result });
36-
})();
38+
};
39+
40+
alternative();
3741

3842
// New approach
3943

40-
const main = async () => {
44+
const modern = async () => {
4145
const { promise, resolve, reject } = Promise.withResolvers();
4246
setTimeout(reject, 1000, new Error('Timed out'));
4347
sumAsync(6, 7, resolve);
4448
const result = await promise;
4549
console.log({ result });
4650
};
4751

48-
main();
52+
modern();

0 commit comments

Comments
 (0)