Skip to content

Instantly share code, notes, and snippets.

@wickdninja
Created March 26, 2018 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wickdninja/d5e99a3c161c1047e411e1364bf7f1e5 to your computer and use it in GitHub Desktop.
Save wickdninja/d5e99a3c161c1047e411e1364bf7f1e5 to your computer and use it in GitHub Desktop.
const asyncThingWithPromise = () => {
var promise = new Promise((resolve, reject) => {
try{
// do something async here. Like get data from a server or read a file from disk etc.
var value = 'My Async Data';
resolve(value) // handled by promise's then()
}catch(error){
reject(error) // handled by promise's catch()
}
});
return promise;
}
// usage
asyncThingWithPromise()
.then(value => console.log(value))
.catch(error => console.error(error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment