Created
May 6, 2020 15:35
-
-
Save medisoft/981b8c7f74e18c0071e1788a6c070cab 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
const f = async (param, callback=false) => { | |
try { | |
const data = await request.get(url); | |
if(callback) callback(null, data); // If callback, then call it | |
return data; // Then return data, it returns as a promise.resolve automatically | |
} catch(err) { | |
if(callback) callback(err); // If callback, then call it | |
throw err; // Then throw, it returns promise.reject automatically | |
} | |
} | |
f(1).then(console.log); // call as promise | |
f(1, console.log); // call as callback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment