Created
March 26, 2018 17:19
-
-
Save wickdninja/d5e99a3c161c1047e411e1364bf7f1e5 to your computer and use it in GitHub Desktop.
This file contains 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 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