Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Legacy function with callbacks
- function doShit(key, callback) {
- if(key == 1) {
- callback(null, 'SUCCESS');
- } else {
- callback(new Error('Not one'), null);
- }
- }
- // Consuming Legacy code
- doShit(1, (error, val) => {
- if(error)
- console.log("Error occured and while doing shit. " + error);
- else
- console.log("Shit is done, horray :D. " + val)
- });
- // Wrapping doShit function with Promise API
- function doShitWithPromise(key) {
- return new Promise((resolve, reject) => {
- doShit(key, (error, val) => {
- if(error) reject(error);
- else resolve(val);
- });
- });
- }
- // Consuming new doShit function style with promise
- var shitPromise = doShitWithPromise(2)
- .then((val) => console.log("Shit is done, horray :D. " + val))
- .catch((error) => console.log("Error occured and while doing shit. " + error));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement