Last active
December 28, 2015 22:17
-
-
Save brunoskonrad/206d25d27253dc9bbd2f 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 'babel-core/register'; | |
import 'babel-polyfill'; | |
function getRandom(from, to, callback) { | |
setTimeout(() => { | |
const value = Math.floor((Math.random() * (to - from + 1)) + from); | |
callback(value); | |
}, 500); | |
} | |
function sum(x, y, callback) { | |
setTimeout(() => { | |
callback(x + y); | |
}, 250); | |
} | |
function display(value, callback) { | |
setTimeout(() => { | |
console.log(value); | |
callback(true); | |
}, 100); | |
} | |
getRandom(1, 6, (value) => { | |
sum(value, 10, (total) => { | |
display(total, (success) => { | |
console.log('callback', success); | |
}); | |
}); | |
}); | |
const Promised = { | |
getRandom(from, to) { | |
return new Promise((resolve) => { | |
getRandom(from, to, (value) => { | |
resolve(value); | |
}); | |
}); | |
}, | |
sum(x, y) { | |
return new Promise((resolve) => { | |
sum(x, y, (value) => { | |
resolve(value); | |
}); | |
}); | |
}, | |
display(value) { | |
return new Promise((resolve) => { | |
display(value, (success) => { | |
resolve(success); | |
}) | |
}); | |
} | |
} | |
Promised.getRandom(1, 6) | |
.then((value) => { | |
return Promised.sum(value, 10); | |
}) | |
.then((total) => { | |
return Promised.display(total); | |
}) | |
.then((success) => { | |
console.log('promise', success); | |
}); | |
async function main() { | |
const random = await Promised.getRandom(1, 6); | |
const total = await Promised.sum(random, 10); | |
const success = await Promised.display(total); | |
console.log('async/await', success); | |
} | |
main(); | |
console.log('start'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment