Skip to content

Commit 22701fe

Browse files
committed
Add examples
1 parent 9907d9b commit 22701fe

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

JavaScript/1-asyncify.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// Synchronous function to callback-last
4+
5+
const asyncify = fn => (...args) => {
6+
const callback = args.pop();
7+
setTimeout(() => {
8+
try {
9+
const result = fn(...args);
10+
if (result instanceof Error) callback(result);
11+
else callback(null, result);
12+
} catch (error) {
13+
callback(error);
14+
}
15+
}, 0);
16+
};
17+
18+
// Usage
19+
20+
const twice = x => x * 2;
21+
const twiceAsync = asyncify(twice);
22+
23+
const half = x => x / 2;
24+
const halfAsync = asyncify(half);
25+
26+
const result = half(twice(100));
27+
console.dir({ sync: result });
28+
29+
twiceAsync(100, (e, value) => {
30+
halfAsync(value, (e, result) => {
31+
console.dir({ asyncified: result });
32+
});
33+
});

JavaScript/2-promisify.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// Callback-last function to Promise-returning
4+
5+
const promisify = fn => (...args) => new Promise(
6+
(resolve, reject) => {
7+
fn(...args, (err, data) => {
8+
if (err) reject(err);
9+
else resolve(data);
10+
});
11+
}
12+
);
13+
14+
// Usage
15+
16+
const twiceCallback = (x, callback) => {
17+
callback(null, x * 2);
18+
};
19+
const twicePromise = promisify(twiceCallback);
20+
21+
const halfCallback = (x, callback) => {
22+
callback(null, x / 2);
23+
};
24+
const halfPromise = promisify(halfCallback);
25+
26+
twiceCallback(100, (e, value) => {
27+
halfCallback(value, (e, result) => {
28+
console.dir({ callbackLast: result });
29+
});
30+
});
31+
32+
twicePromise(100)
33+
.then(value => halfPromise(value))
34+
.then(result => {
35+
console.dir({ promisified: result });
36+
});

JavaScript/3-promisify-sync.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
// Synchronous function to Promise-returning
4+
5+
const promisifySync = fn => (...args) => {
6+
try {
7+
const result = fn(...args);
8+
if (result instanceof Error) return Promise.reject(result);
9+
else return Promise.resolve(result);
10+
} catch (error) {
11+
return Promise.reject(error);
12+
}
13+
};
14+
15+
// Usage
16+
17+
const twice = x => x * 2;
18+
const twicePromise = promisifySync(twice);
19+
20+
const half = x => x / 2;
21+
const halfPromise = promisifySync(half);
22+
23+
const result = half(twice(100));
24+
console.dir({ sync: result });
25+
26+
twicePromise(100)
27+
.then(value => halfPromise(value))
28+
.then(result => {
29+
console.dir({ promise: result });
30+
});

JavaScript/4-callbackify.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// Promise-returning function callback-last / error-first
4+
5+
const callbackify = fn => (...args) => {
6+
const callback = args.pop();
7+
fn(...args)
8+
.then(value => {
9+
callback(null, value);
10+
})
11+
.catch(reason => {
12+
callback(reason);
13+
});
14+
};
15+
16+
// Usage
17+
18+
const twicePromise = x => Promise.resolve(x * 2);
19+
const twiceCallback = callbackify(twicePromise);
20+
21+
const halfPromise = x => Promise.resolve(x / 2);
22+
const halfCallback = callbackify(halfPromise);
23+
24+
twicePromise(100)
25+
.then(value => halfPromise(value))
26+
.then(result => {
27+
console.dir({ promise: result });
28+
});
29+
30+
twiceCallback(100, (e, value) => {
31+
halfCallback(value, (e, result) => {
32+
console.dir({ callbackLast: result });
33+
});
34+
});

JavaScript/5-promise-to-cb.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// Convert Promise to callback-last
4+
5+
const promiseToCallbackLast = promise => callback => {
6+
promise.then(value => {
7+
callback(null, value);
8+
}).catch(reason => {
9+
callback(reason);
10+
});
11+
};
12+
13+
// Usage
14+
15+
const promise = Promise.resolve('value');
16+
const fnCallbackLast = promiseToCallbackLast(promise);
17+
18+
fnCallbackLast((e, result) => {
19+
console.dir({ callbackLast: result });
20+
});

0 commit comments

Comments
 (0)